将Ajax添加到我的PHP分页中

时间:2017-06-07 04:59:33

标签: php jquery ajax pdo pagination

我有一个分页系统,我想在不重新加载整个页面的情况下更改页码,以提高网站速度。

这是我的分页代码(没有ajax)

<?php
$stmt = $conn->prepare("SELECT name FROM info LIMIT $limit OFFSET $start");
     // Then fire it up
     $stmt->execute();
     // Pick up the result as an array
     $result = $stmt->fetchAll();
    // Now you run through this array in many ways, for example
     for($x=0, $n=count($result); $x < $n; $x++){


         echo " <p>" .$result[$x]['movie_name']. "</p>";
     }



//pagination
 echo '<div onchange="showUser(this.value)">';
while ($pagenumber != $totalpages) 
{
++$pagenumber;

echo "<a href='?page=".$pagenumber."' >".$pagenumber."</a>";  
}
?> 
</div>

我尝试向它添加ajax:这是我的代码,但它不起作用。

<head>
<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("pagination").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("pagination").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","a.php?page="+str,true);
  xmlhttp.send();
}
</script>
</head> 
<body>

$stmt = $conn->prepare("SELECT name FROM info LIMIT $limit OFFSET $start");
     // Then fire it up
     $stmt->execute();
     // Pick up the result as an array
     $result = $stmt->fetchAll();
    // Now you run through this array in many ways, for example
     for($x=0, $n=count($result); $x < $n; $x++){


         echo "   
<div onchange='showUser(this.value)'>
<p id=pagination>" .$result[$x]['movie_name']. "</p>
</div>
";
     }


//pagination
 echo '<div onchange="showUser(this.value)">';
while ($pagenumber != $totalpages) 
{
++$pagenumber;

echo "<a href='?page=".$pagenumber."' >".$pagenumber."</a>";  
}

?>
</div>

</body>

0 个答案:

没有答案