php Ajax实时搜索在搜索一个值后不起作用

ryoqjall  于 5个月前  发布在  PHP
关注(0)|答案(2)|浏览(62)

我的网页需要刷新后,每次搜索页面。请任何帮助。我bignner为Ajax和这里是我的索引页代码。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function(){

    $("#live_search").keyup(function(){
      var input = $(this).val();
      //alert(input);

      if(input != ""){
        $.ajax({

          url:"livesearch.php",
          method:"POST",
          data:{input:input},

          success:function(data){
           $("#searchresult").html(data);
          }
        });
      }else{
        $("#searchresult").css("display", "none"); 
      }
    });
  });
    
</script>

字符串
这是我的PHP代码

<?php


includes('our/db_connect.php');
if($_POST 'input']){

$input = $_POST['input'];
$query = "SELECT * FROM parcels WHERE reference_number LIKE '{$input}'";
$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) > 0){?>

        <table class="table table-bordered table-striped mt-4">
            <thead>
                <tr>
                    <th>tracking num</th>
                    <th>sender contact</th>
                    <th>status</th>
                    <th>From</th>
                    <th>To</th>
                </tr>
            </thead>
            <tbody>
                <?php
                while($row = mysqli_fetch_assoc($result)){
                    $ref = $row['reference_number'];
                    $sender = $row['sender_name'];
                    $from = $row['from_branch_id'];
                    $to = $row['to_branch_id'];

                    ?>
                    <tr>
                        <td><?php echo $ref;?></td>
                        <td><?php echo $sender;?></td>
                        <td><?php echo $from;?></td>
                        <td><?php echo $to;?></td>
                    </tr>

                    <?php
                }


?>

</tbody>
        </table>
    <?php 
    
}else{
    echo "No Parcel Found";
}


}中的值?>

92dk7w1h

92dk7w1h1#

你隐藏了div,但当你得到一个不好的结果时,你不会再显示它。
编写下面的代码:

if (input != '') {
    $.ajax({
        url: 'livesearch.php',
        method: 'POST',
        data: { input: input },
        success: function (data) {
            $('#searchresult').html(data);
            $('#searchresult').show();
        },
    });
} else {
    $('#searchresult').hide();
}

字符串
这里我再次展示了$('#searchresult'),在您的响应之后,您替换了$('#searchresult')中的HTML

pobjuy32

pobjuy322#

由于搜索后,首先,然后清空您的搜索输入使用keyup键盘事件和输入value == ""有你隐藏搜索结果ID和你在此代码不添加代码显示搜索结果ID后,空输入值,当你再次搜索,所以你必须添加一个显示或display = 'block'显示在下面的代码。

$(document).ready(function () {
    $("#live_search").keyup(function () {
      var input = $(this).val();
      //alert(input);
  
      if (input != "") {
        $.ajax({
          url: "livesearch.php",
          method: "POST",
          data: { input: input },
          success: function (data) {
            $("#searchresult").html(data);
            $("#searchresult").show();
            //Or add
            $("#searchresult").css("display", "block");
          }
        });
      } else {
        $("#searchresult").css("display", "none");
      }
    });
  });

字符串

相关问题