Jump to content

ajax and php


etsted
 Share

Recommended Posts

i have a pagination script with ajax and php, but i doesnt show anything on the page, not even erros.

 

ajax.pagination.php

 

<?phpinclude_once("connect.php");// This first query is just to get the total count of rows$sql = "SELECT COUNT(id) FROM data";$query = mysqli_query($con, $sql) or die(mysqli_error());$row = mysqli_fetch_row($query);// Here we have the total row count$total_rows = $row[0];// Specify how many results per page$rpp = 10;// This tells us the page number of our last page$last = ceil($total_rows/$rpp);// This makes sure $last cannot be less than 1if($last < 1){ $last = 1; } // Close the database connection mysqli_close($con); ?> <!DOCTYPE html> <html> <head> <script> var rpp = <?php echo $rpp; ?>; // results per page var last = <?php echo $last; ?>; // last page number function request_page(pn) { var results_box = document.getElementById("results_box"); var pagination_controls = document.getElementById("pagination_controls"); results_box.innerHTML = "loading results ..."; var hr = new XMLHttpRequest(); hr.open("POST", "pagination_parser.php", true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if(hr.readyState == 4 && hr.status == 200) { var dataArray = hr.responseText.split("||"); var html_output = ""; for(i = 0; i < dataArray.length - 1; i++) { var itemArray = dataArray.split("|"); html_output += "ID: "+itemArray[0]+" - Testimonial from <b>"+itemArray[1]+"</b><hr>"; } results_box.innerHTML = html_output; } } hr.send("rpp="+rpp+"&last="+last+"&pn="+pn); // Change the pagination controls var paginationCtrls = ""; // Only if there is more than 1 page worth of results give the user pagination controls if(last != 1) { if (pn > 1) { paginationCtrls += '<button onclick="request_page('+(pn-1)+')"><</button>'; } paginationCtrls += '     <b>Page '+pn+' of '+last+'</b>     '; if (pn != last) { paginationCtrls += '<button onclick="request_page('+(pn+1)+')">></button>'; } } pagination_controls.innerHTML = paginationCtrls; } </script> </head> <body> <div id="pagination_controls"></div> <div id="results_box"></div> <script> request_page(1); </script> </body> </html>

 

 

pagination.parser.php

 

<?php// Make the script run only if there is a page number posted to this scriptif(isset($_POST['pn'])) { $rpp = preg_replace('#[^0-9]#', '', $_POST['rpp']); $last = preg_replace('#[^0-9]#', '', $_POST['last']); $pn = preg_replace('#[^0-9]#', '', $_POST['pn']); // This makes sure the page number isn't below 1, or more than our $last page if ($pn < 1) { $pn = 1; } else if ($pn > $last) { $pn = $last; } // Connect to our database here include_once("connect.php"); // This sets the range of rows to query for the chosen $pn $limit = 'LIMIT ' .($pn - 1) * $rpp .',' .$rpp; // This is your query again, it is for grabbing just one page worth of rows by applying $limit $sql = "SELECT id, navn FROM data ORDER BY id DESC $limit"; $query = mysqli_query($con, $sql) or die(mysqli_error($con)); $dataString = ''; while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { $id = $row["id"]; $navn = $row["navn"]; $dataString .= $id.'|'.$navn.'||'; } // Close your database connection mysqli_close($con); // Echo the results back to Ajax echo $dataString; exit(); } ?>

Edited by etsted
Link to comment
Share on other sites

i have a pagination script with ajax and php, but i doesnt show anything on the page, not even erros.

Well, in all fairness, you aren't checking for, or displaying errors.

 

When developing, you should be displaying errors, by putting this at the top of your script

 

ini_set('display_errors', 'on');

 

With SQL, you should always be handling and checking for errors.

http://www.w3schools.com/php/func_mysqli_error.asp

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...