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

ryoqjall  于 2024-01-05  发布在  PHP
关注(0)|答案(2)|浏览(223)

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

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function(){
  4. $("#live_search").keyup(function(){
  5. var input = $(this).val();
  6. //alert(input);
  7. if(input != ""){
  8. $.ajax({
  9. url:"livesearch.php",
  10. method:"POST",
  11. data:{input:input},
  12. success:function(data){
  13. $("#searchresult").html(data);
  14. }
  15. });
  16. }else{
  17. $("#searchresult").css("display", "none");
  18. }
  19. });
  20. });
  21. </script>

字符串
这是我的PHP代码

  1. <?php


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

  1. $input = $_POST['input'];
  2. $query = "SELECT * FROM parcels WHERE reference_number LIKE '{$input}'";
  3. $result = mysqli_query($conn, $query);
  4. if(mysqli_num_rows($result) > 0){?>
  5. <table class="table table-bordered table-striped mt-4">
  6. <thead>
  7. <tr>
  8. <th>tracking num</th>
  9. <th>sender contact</th>
  10. <th>status</th>
  11. <th>From</th>
  12. <th>To</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <?php
  17. while($row = mysqli_fetch_assoc($result)){
  18. $ref = $row['reference_number'];
  19. $sender = $row['sender_name'];
  20. $from = $row['from_branch_id'];
  21. $to = $row['to_branch_id'];
  22. ?>
  23. <tr>
  24. <td><?php echo $ref;?></td>
  25. <td><?php echo $sender;?></td>
  26. <td><?php echo $from;?></td>
  27. <td><?php echo $to;?></td>
  28. </tr>
  29. <?php
  30. }


?>

  1. </tbody>
  2. </table>
  3. <?php
  4. }else{
  5. echo "No Parcel Found";
  6. }


}中的值?>

92dk7w1h

92dk7w1h1#

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

  1. if (input != '') {
  2. $.ajax({
  3. url: 'livesearch.php',
  4. method: 'POST',
  5. data: { input: input },
  6. success: function (data) {
  7. $('#searchresult').html(data);
  8. $('#searchresult').show();
  9. },
  10. });
  11. } else {
  12. $('#searchresult').hide();
  13. }

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

展开查看全部
pobjuy32

pobjuy322#

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

  1. $(document).ready(function () {
  2. $("#live_search").keyup(function () {
  3. var input = $(this).val();
  4. //alert(input);
  5. if (input != "") {
  6. $.ajax({
  7. url: "livesearch.php",
  8. method: "POST",
  9. data: { input: input },
  10. success: function (data) {
  11. $("#searchresult").html(data);
  12. $("#searchresult").show();
  13. //Or add
  14. $("#searchresult").css("display", "block");
  15. }
  16. });
  17. } else {
  18. $("#searchresult").css("display", "none");
  19. }
  20. });
  21. });

字符串

展开查看全部

相关问题