doupao5296 2018-09-15 17:07
浏览 47

从没有使用PHP MySQL的用户输入向下滚动时,AJAX会加载更多结果

I am trying to get results from user input using AJAX / PHP / MySQL

Here are my codes

  <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 } ?>

Here's the codes from 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 } } ?>

Now the problem is when I input any keyword it gives me results and when i scroll down it gives me results but not relevant I mean lets say I put hello world so the main page shows results for hello world but when I scroll down it gives me result not according to the input.

What am I doing wrong here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 linux驱动,linux应用,多线程
    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助