如何将我的正常分页转换为ajax分页

时间:2017-06-07 06:51:36

标签: javascript php jquery ajax pagination

这是我的分页代码:

<?php
$stmt = $conn->prepare("SELECT * 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   
while ($pagenumber != $totalpages) 
    {
    ++$pagenumber;
    echo "<a href='?page=".$pagenumber."'>".$pagenumber."</a>";  
    }    
?>

Ajax需要一些javascript技能,而且我不懂JS。我不知道,如何将其转换为Ajax分页。我看了一些w3schools教程和其他一些教程,但仍然无法将其转换为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 个答案:

没有答案