使查询选择喜欢用户名并触发模式隐藏 - Ajax & Jquery



如何成功从服务器获取数据后,如何触发模态隐藏。现在,我的问题是即使在服务器端进行查询之后,模式也没有关闭,这可能我可能是我的Ajax调用是错误的,但尝试了此代码,但它甚至无法正常工作,请在下面查看我的代码。

这是值

<textarea id="scanned-QR" name="search"></textarea>

这是模态

    <!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
      </div>
      <div class="modal-body">
        ...
      </div>
    </div>
  </div>
</div>

这是我从textarea值中获取的var值

var query = $('#scanned-QR').val();
    fetch_customer_data(query);

$(document).on('keyup', '#scanned-QR', function(){
      var query = $(this).val();
      fetch_customer_data(query);
    });

这是我的ajax调用

    function fetch_customer_data(query = '') 
    {
      $.ajax({
        url:"select.php",
        method: 'GET',
        data:{query:query},
        dataType: 'json',
        success:function(data) {
            $("#exampleModal").removeClass("in");
            $(".modal-backdrop").remove();
            $('body').removeClass('modal-open');
            $('body').css('padding-right', '');
            $("#exampleModal").hide();
        },
        error:function(err){
            console.log(err);
        }
      });
    }

因此,输出为:在i keyup之后,如果 LIKE或相同的值,则TextArea值将与数据库值进行比较,则如果它是相同的值。成功AJAX必须执行以下行。

最后,我的数据库连接和我的选择查询

select.php

<?php
  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
  error_reporting(E_ALL);
  $link = mysqli_connect("localhost","root","");
  mysqli_select_db($link, "test");
  $query = $_GET['query'];
  $res = mysqli_query($link,"SELECT * FROM admin WHERE username LIKE '$admin%' ");
  ?>

有两个错误,

1(将$('#exampleModal').hide();替换为$('#exampleModalLong').hide();

2(您没有从 select.php

返回json

select.php中添加此

if (mysqli_num_rows($res) > 0) {
    $respose = array('status'=>'1');//1 for success
    echo json_encode($respose );        
} else {
    $respose = array('status'=>'0');//0 for fail
    echo json_encode($respose );        
}
mysqli_close($link);

在ajax中:添加此

success:function(data) {
   if(data.status == '1'){
       $("#exampleModalLong").removeClass("in");
       $(".modal-backdrop").remove();
       $('body').removeClass('modal-open');
       $('body').css('padding-right', '');
       $("#exampleModalLong").hide();
   }
},

最新更新