我的分页代码存在问题。我有一个PHP文件,用于过滤和显示搜索结果。它第一次正常工作,但是当我点击分页链接ie.next,previous等时,它显示q,c b,m和n的未定义索引错误。 我正在使用ajax帖子
<?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> ";
}
}
?>
ajax poxt代码:
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);
}