1

I'm Having problems with my pagination code. I Have a PHP file which filters and displays search results. it works fine for the first time but when i click on the pagination links ie.next, previous etc it shows undefined index error for q, c b, m and n. I'm using ajax post

<?php
include("db_connect.inc");


$name=$_POST['q'];
$categories=$_POST['c'];
$brand=$_POST['b'];
$min=$_POST['m'];
$max=$_POST['n'];


$sql="SELECT name, category, description, price, quantity, brand FROM products";

$query="";
if($name != "")
{
    $query .= " name = '$name'";
}
if ($categories != "")
{ 
    if(!empty($query))
    {
    $query.= " AND ";
    }
    $query .=" category ='$categories' ";


}
if ($brand != "")
{    
       if(!empty($query))
    {
    $query.= " AND ";
    }
    $query.= " brand = '$brand' ";
}
if ($min != "")
{    
     if(!empty($query))
    {
    $query.= " AND ";
    }
    $query.= "price BETWEEN '$min' AND '$max' ";
}

if(!empty($query))
{
$query = " WHERE ".$query;
$sql1=$sql.$query;



                    $page=1;


$result = mysql_query($sql1) or die(mysql_error());
$total_records=mysql_num_rows($result);

$total_pages = ceil($total_records / 2);

$page = (int)$page;
                    if ($page > $total_pages) {
                    $page = $total_pages;
                    } 
                      if ($page < 1) {
                    $page= 1;
                    } 

                    $start_from = ($page-1) * 2;
$q=$sql1."limit $start_from,2";
$q=mysql_query($q) or die(mysql_error());


echo "<center>";
echo "<table border='2' bgcolor=#CCCCCC>
<tr>
<th>NAME</th>
<th>CATEGORY</th>
<th>DESCRIPTION</th>
<th>PRICE</th>
<th>QUANTITY</th>
<th>BRAND</th>
</tr>";

while($row=mysql_fetch_array($q))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
}
echo "</table>";
echo "<center>";


              if ($page== 1) {
              echo " << < ";
              } else {
              echo " <a href='{$_SERVER['PHP_SELF']}?page=1'><<</a> ";
              $prevpage = $page-1;
               echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'><</a> ";
             } 
             echo " ( Page [$page] of [$total_pages] ) ";

             if ($page == $total_pages) {
             echo " > >> ";
             } else {
              $nextpage = $page+1;
              echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>></a> ";
              $lastpage=$total_pages;
              echo " <a href='{$_SERVER['PHP_SELF']}?page=$lastpage'>>></a> ";
              } 



}
?>

the ajax poxt code:

document.getElementById("but").onclick= function()
{
var name= document.getElementById("name").value;
var ct=document.getElementById("categories").value;
var br=document.getElementById("brand").value;
var mn= document.getElementById("min").value;
var mx= document.getElementById("max").value;
var xmlhttp;

xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById("para").innerHTML= xmlhttp.responseText;    
}
xmlhttp.open("POST","view.php" ,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("q="+name+ "&c="+ct+ "&b="+br+ "&m="+mn+ "&n="+mx);

}
  • You should also show the ajax post code. – Hunter WebDev May 28 '14 at 17:47
  • I've updated the ajax post code. – confusedguy May 28 '14 at 17:52
  • I don't see an error in your XMLhttprequest code. Try adding the `Content-length` and `Connection` headers as well. Check [here](http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest) how. –  May 28 '14 at 18:02
  • still the same error. it actually displays the results when i clickcon the button .the errors arise when i click on the pagination links. – confusedguy May 28 '14 at 18:14
  • Side note: mysql_* functions are deprecated. You should look into mysqli or PDO, with prepared statements. You're vulnerable to [Bobby Tables](http://bobby-tables.com) as is. Also, you should use `htmlentities()` on `PHP_SELF` to prevent XSS vulnerabilities. – Matthew Johnson May 28 '14 at 19:07
  • I assigned $self=htmlentities($_SERVER['PHP_SELF']); and replaced all $_SERVER['PHP_SELF'] with $self, still the same errors. Am i doing it right? Sorry this is my first pagination code and I'm new to this. – confusedguy May 29 '14 at 06:30

0 Answers0