-1

我正在使用 keyup() 函数将变量发送到具有 mysql 选择的 php 页面 (consult.php) 以带来我的搜索结果。

索引.php

<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
 $('#input').keyup(function(){
  var inputVal=$('#input').val();
  $.ajax({
   type: 'POST',
   data: ({word : inputVal}),
   url: 'consult.php',
   success: function(data) {
    $('#content').html(data);
   }
  });
 });
 $("#menu li").click(function(){
  $.ajax({
   type: 'POST',
   data: ({word : inputVal}),
   url: 'consult.php',
   success: function(data) {
    $('#content').html(data);
  }
  });
 });
});
</script>
<style>
 #menu ul{
  margin:0;
  padding:0;
  list-style:none;
 }
 #menu ul li{
  padding:5px;
  margin-right:2px;
  float:left;
  border:#00F solid 1px;
  background-color:#faa8ad;
}
</style>
</head>
<body>
 <div>Write<br>
  <input type="text" id="input" name="input"/>
 </div>
<div id="content"></div>
</body>
</html>

咨询.php

<?
 include('conn.php');
 $word = isset($_POST['word']) ? $_POST['word'] : $_GET['word'];
 if($word){

  $perPag = 4;
  $query = mysql_query("SELECT COUNT(carro_id) FROM carros WHERE carro_modelo LIKE '$word%'");
  $totalResult = ceil(mysql_result($query, 0)/$perPag);

  $pag = (isset($_GET['pag']) and (int)$_GET['pag'] > 0) ? (int)$_GET['pag'] : 1;
  $ini = ($pag - 1) * $perPag;

  $queryTotal = mysql_query("SELECT carro_modelo FROM carros WHERE carro_modelo LIKE '$word%' ORDER BY carro_id LIMIT $ini, $perPag");
      while($row = mysql_fetch_assoc($queryTotal)){
       echo "<div>".$row['carro_modelo']."</div>";
      }
  if($totalResult >= 1 && $pag <= $totalResult){
   for($i = 1; $i <= $totalResult; $i++){
    echo($i == $pag) ? '<div id="menu"><ul><li><strong><a href="?pag='.$i.'&word='.$word.'">'.$i.'</a></strong></li></ul></div> ' : '<div id="menu"><ul><li><a href="?pag='.$i.'&word='.$word.'">'.$i.'</a></li></ul></div> ';
   }
  }
 }else{
  $queryTotalReult = mysql_query("SELECT * FROM carros");
  while($rowTotal = mysql_fetch_assoc($queryTotalReult)){
   echo "<div>".$rowTotal['carro_modelo']."</div>";
  }
 }
?>

问题是,当我点击分页的链接时,它会加载返回 index.php 中的结果,从而再次忽略搜索。

先感谢您!

4

2 回答 2

2

索引.php

<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
function ajaxCall(inPage,inWord)
{

      $.ajax({
       type: 'POST',
       data: ({word : inWord, pag : inPage}),
       url: 'consult.php',
       success: function(data) {
        $('#content').html(data);
       }});
}
    $(function(){
     $('#input').keyup(function(){
      var inputVal=$('#input').val();
      $.ajax({
       type: 'POST',
       data: ({word : inputVal}),
       url: 'consult.php',
       success: function(data) {
        $('#content').html(data);
       }
      });
     });
    });
    </script>
    <style>
     #menu ul{
      margin:0;
      padding:0;
      list-style:none;
     }
     #menu ul li{
      padding:5px;
      margin-right:2px;
      float:left;
      border:#00F solid 1px;
      background-color:#faa8ad;
    }
    </style>
    </head>
    <body>
     <div>Write<br>
      <input type="text" id="input" name="input"/>
     </div>
    <div id="content"></div>
    </body>
    </html>

咨询.php

<?
 include('conn.php');
 $word = isset($_POST['word']) ? $_POST['word'] : $_GET['word'];
 if($word){
  $perPag = 4;
  $query = mysql_query("SELECT COUNT(carro_id) FROM carros WHERE carro_modelo LIKE '$word%'");
  $totalResult = ceil(mysql_result($query, 0)/$perPag);
  $pag = (isset($_GET['pag']) and (int)$_GET['pag'] > 0) ? (int)$_GET['pag'] : 1;
  $ini = ($pag - 1) * $perPag;
  $queryTotal = mysql_query("SELECT carro_modelo FROM carros WHERE carro_modelo LIKE '$word%' ORDER BY carro_id LIMIT $ini, $perPag");
      while($row = mysql_fetch_assoc($queryTotal)){
       echo "<div>".$row['carro_modelo']."</div>";
      }
  if($totalResult >= 1 && $pag <= $totalResult){
   for($i = 1; $i <= $totalResult; $i++){
    echo($i == $pag) ? '<div id="menu"><ul><li><strong><a onclick="ajaxCall('.$i.',\''.$word.'\')">'.$i.'</a></strong></li></ul></div> ' : '<div id="menu"><ul><li><a onclick="ajaxCall('.$i.',\''.$word.'\')">'.$i.'</a></li></ul></div> ';
   }
  }
 }else{
  $queryTotalReult = mysql_query("SELECT * FROM carros");
  while($rowTotal = mysql_fetch_assoc($queryTotalReult)){
   echo "<div>".$rowTotal['carro_modelo']."</div>";
  }
 }
?>
于 2012-05-01T16:15:07.487 回答
1

在您的查询中您有 $initial,但在作业中您有 $ini。也许尝试使这些相同:)

于 2012-04-30T21:33:12.643 回答