【问题标题】:Cannot show error message "No result Found" in php-ajax-live-search-with-multiple-value无法在 php-ajax-live-search-with-multiple-value 中显示错误消息“未找到结果”
【发布时间】:2020-11-25 09:39:46
【问题描述】:

我在https://www.webslesson.info/2019/03/php-ajax-live-search-with-multiple-value.html上关注了这个教程

在我的本地计算机上托管后一切正常。除非我搜索数据库上不可用的数据,否则我想收到错误消息“在此表上找不到结果”。 请检查代码。

您也可以在这里查看演示,http://demo.webslesson.info/bootstrap-tags-input-with-php/。 "搜索无效值时不会弹出未找到结果错误。

索引.php

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Live Data Search using Multiple Tag in PHP with Ajax</title>  
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.css" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput-typeahead.css" />
  <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-tagsinput/0.8.0/bootstrap-tagsinput.min.js" crossorigin="anonymous"></script>
  
  <style>
  .bootstrap-tagsinput {
   width: 100%;
  }
  </style>
 </head>
 <body>
  <div class="container">
   <br />
   <br />
   <br />
   <h2 align="center">Live Data Search using Multiple Tag in PHP with Ajax</h2><br />
   <div class="form-group">
    <div class="row">
     <div class="col-md-10">
      <input type="text" id="tags" class="form-control" data-role="tagsinput" />
     </div>
     <div class="col-md-2">
      <button type="button" name="search" class="btn btn-primary" id="search">Search</button>
     </div>
    </div>
   </div>
   <br />
   <div class="table-responsive">
    <div align="right">
     <p><b>Total Records - <span id="total_records"></span></b></p>
    </div>
    <table class="table table-bordered table-striped">
     <thead>
      <tr>
       <th>Customer Name</th>
       <th>Gender</th>
       <th>Address</th>
       <th>City</th>
       <th>Postal Code</th>
       <th>Country</th>
      </tr>
     </thead>
     <tbody>
     </tbody>
    </table>
   </div>
  </div>
  <div style="clear:both"></div>
  <br />
  
  <br />
  <br />
  <br />
 </body>
</html>


<script>
$(document).ready(function(){
 
 load_data();

 function load_data(query)
 {
  $.ajax({
   url:"fetch.php",
   method:"POST",
   data:{query:query},
   dataType:"json",
   success:function(data)
   {
    $('#total_records').text(data.length);
    var html = '';
    if(data.length > 0)
    {
     for(var count = 0; count < data.length; count++)
     {
      html += '<tr>';
      html += '<td>'+data[count].CustomerName+'</td>';
      html += '<td>'+data[count].Gender+'</td>';
      html += '<td>'+data[count].Address+'</td>';
      html += '<td>'+data[count].City+'</td>';
      html += '<td>'+data[count].PostalCode+'</td>';
      html += '<td>'+data[count].Country+'</td></tr>';
     }
    }
    else
    {
     html = '<tr><td colspan="5">No Data Found</td></tr>';
    }
    $('tbody').html(html);
   }
  })
 }

 $('#search').click(function(){
  var query = $('#tags').val();
  load_data(query);
 });

});
</script>

Fetch.php

    <?php

//fetch.php

$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");

$output = '';

$query = '';

if(isset($_POST["query"]))
{
 $search = str_replace(",", "|", $_POST["query"]);
 $query = "
 SELECT * FROM tbl_customer 
 WHERE CustomerName REGEXP '".$search."' 
 OR Address REGEXP '".$search."' 
 OR City REGEXP '".$search."' 
 OR PostalCode REGEXP '".$search."' 
 OR Country REGEXP '".$search."'
 ";
}
else
{
 $query = "
 SELECT * FROM tbl_customer ORDER BY CustomerID
 ";
}

$statement = $connect->prepare($query);
$statement->execute();

while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
 $data[] = $row;
}

echo json_encode($data);

?>

【问题讨论】:

  • 如果您在浏览器控制台中签入,您可能会收到 Uncaught TypeError: Cannot read property 'length' of null 。因为没有结果,所以您尝试打印 $('#total_records').text(data.length); 将该部分放入 if(data.length)... 并查看一次
  • 是的,你是对的,在我的浏览器中看到了 Unacugh 类型错误。你能帮我把那条线怎么放。我还是进不去。 if(data.length > 0) $('#total_records').text(data.length);
  • 放在 fetch.php 的顶部 $data = [];所以这个 var 总是存在 en 将被返回到你的 javascript
  • 感谢@IvoP。它可以工作,但它不会在 html table 下显示错误消息。实际上,这 { html = 'No Data Found'; } 仍然没有工作。有什么想法吗?
  • 在没有找到匹配结果时检查data内部有什么成功功能,即:alert(data)

标签: javascript php html mysql ajax


【解决方案1】:

作为将来参考我在超过 1 条评论行中的回答:

在 Fetch.php 中,变量 $data 仅在找到一个或多个答案的情况下才会存在。 否则,最后一行仍然不存在 var $data: json_encode($data); 您是否可以打开 error_reporting,它会给您一条关于“第 X 行未定义的 var 数据”的消息。

PHP 会原谅你,假设 $data 的值为 NULL;所以你得到一个 json_encoded 形式或 NULL。

因此,如果您将 Fetch.php 放在顶部,它将始终存在并且是一个空数组或填充数组。

这将避免您在 Javascript 中无法读取 NULL 长度的错误。 然而,一个空数组的长度 = 0。

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 2012-05-31
    • 2013-09-24
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多