从用户输入向下滚动时,AJAX加载更多结果,但不适用于PHP MySQL

时间:2018-09-15 17:07:55

标签: php mysql ajax

我正在尝试使用AJAX / PHP / MySQL

从用户输入中获取结果

这是我的密码

  <script type="text/javascript">
    $(window).scroll(function ()
    {
      if($(document).height() <= $(window).scrollTop() + $(window).height())
      {
        loadmore();
      }
    });

    function loadmore()
    {
      var val = document.getElementById("row_no").value;
      $.ajax({
      type: 'post',
      url: 'get_results.php',
      data: {
       getresult:val
      },
      success: function (response) {
      var content = document.getElementById("all_rows");
      content.innerHTML = content.innerHTML+response;

      // We increase the value by 10 because we limit the results by 10
      document.getElementById("row_no").value = Number(val)+10;
      }
      });
    }
</script>

<?php 
$order = $_POST['options'];

if($order == 'seed') $sort = "WHERE MATCH(title) AGAINST('$key') ORDER BY seed DESC";

if($order == 'games') $sort = "WHERE MATCH(title) AGAINST('$key') and category = 'Games' ORDER BY id DESC";

if($order == 'time') $sort = "WHERE MATCH(title) AGAINST('$key') ORDER BY time DESC";
if($order == 'none') $sort = "WHERE MATCH(title) AGAINST('$key') ORDER BY MATCH(title) AGAINST('$key') DESC";
?>

<form action="/load.php" method="post" class="search"><fieldset>
    <input type="text" name="q" value="<?php echo stripslashes(!empty($_POST['q'])) ? $_POST['q'] : null; ?>" id="thesearchbox" required />
        <select name="options" id="thesearchbutton">
            <option value="none">Sort by</option>
            <option value="seed">Seeds</option>
            <option value="time">Time</option>
            <option value="games">Games</option>
        </select>   
    <input type="submit" value="Search" id="thesearchbutton" /></fieldset>
</form>
<?php if($key == ''){}else{?>
  <div id="all_rows">
    <?php
      $select = mysqli_query($conn, "SELECT title,size,time,category FROM data $sort LIMIT 0,10");
      while($row = mysqli_fetch_array($select))
      {
    ?>
       <p class="rows">
            <?php echo $row['title'];?><br />
            <?php echo $row['size'];?><br />
            <?php echo $row['category'];?><br />            
            <?php echo humanTiming(strtotime($row['time']));?><br />        
       </p>
    <?php } ?>
  </div>
  <input type="hidden" id="row_no" value="10">
<?php } ?>

这是来自get_results.php的代码

<?php
    include 'files/config.php';
    include 'files/time.php';

  if(isset($_POST['getresult']))
  {
    $no = $_POST['getresult'];
    /*I even tried this query*/ $select = mysqli_query($conn, "select title from data WHERE MATCH(title) AGAINST('$key') ORDER BY MATCH(title) AGAINST('$key') DESC limit $no,10");
    $select = mysqli_query($conn, "select title,size,time,category from data order by id desc limit $no,5");
    while($row = mysqli_fetch_array($select))
      {
    ?>
   <p class="rows">
        <?php echo $row['title'];?><br />
        <?php echo $row['size'];?><br />
        <?php echo $row['category'];?><br />
        <?php echo humanTiming(strtotime($row['time']));?><br />            
   </p>
<?php } } ?>

现在的问题是,当我输入任何关键字时,它会给我结果;当我向下滚动时,它会给我结果,但不相关。我的意思是说我放了hello world,所以主页显示了{{1} },但是当我向下滚动时,结果却与输入不符。

我在这里做错了什么?

0 个答案:

没有答案