weixin_33720078 2017-11-05 19:51 采纳率: 0%
浏览 45

没有数据信息显示

this code,i have done searching through enroll number. All code are correct and running properly but when match data are not found. No message shown .so, i want to show a message when match data are not found like that "No Such Data Found". How can i do this? please help to solve that problem, My code is below.

index.php

    <!DOCTYPE html>
<html>
 <head>
  <title>How to return JSON Data from PHP Script using Ajax Jquery</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  #result {
   position: absolute;
   width: 100%;
   max-width:870px;
   cursor: pointer;
   overflow-y: auto;
   max-height: 400px;
   box-sizing: border-box;
   z-index: 1001;
  }
  .link-class:hover{
   background-color:#f1f1f1;
  }
  </style>
 </head>
 <body>
  <br /><br />
  <div class="container" style="width:900px;">
   <h2 align="center">How to return JSON Data from PHP Script using Ajax Jquery</h2>
   <h3 align="center">Search Employee Data</h3><br />   
   <div class="row">
    <div class="col-md-4">
     <input type="text" name="employee_list" id="employee_list" class="form-control">

    </div>
    <div class="col-md-4">
     <button type="button" name="search" id="search" class="btn btn-info">Search</button>
    </div>
   </div>
   <br />
   <div class="table-responsive" id="employee_details" style="display:none">
   <table class="table table-bordered">
    <tr>
     <td width="10%" align="right"><b>Name</b></td>
     <td width="90%"><span id="name"></span></td>
    </tr>
    <tr>
     <td width="10%" align="right"><b>Address</b></td>
     <td width="90%"><span id="address"></span></td>
    </tr>

    <tr>
     <td width="10%" align="right"><b>Gender</b></td>
     <td width="90%"><span id="total_marks"></span></td>
    </tr>
    <tr>
     <td width="10%" align="right"><b>Designation</b></td>
     <td width="90%"><span id="email"></span></td>
    </tr>
    <tr>
     <td width="10%" align="right"><b>Age</b></td>
     <td width="90%"><span id="ph"></span></td>
    </tr>
   </table>
   </div>

  </div>
 </body>
</html>

<script>
$(document).ready(function(){
 $('#search').click(function(){
  var enroll= $('#employee_list').val();
  if(enroll != '')
  {
   $.ajax({
    url:"fetch.php",
    method:"POST",
    data:{enroll:enroll},
    dataType:"JSON",
    success:function(data)
    {
    if(data.length != 0){

     $('#employee_details').css("display", "block");
     $('#name').text(data.name);
     $('#address').text(data.address);
     $('#total_marks').text(data.total_marks);
     $('#ph').text(data.ph);
     $('#email').text(data.email);
    }
    else { alert("Please Select Employee"); }
   }

   })
  }

  else
  {
   alert("Please Select Employee");
   $('#employee_details').css("display", "none");
  }
 });
});
</script>

fetch.php

  <?php
//fetch.php
if(isset($_POST["enroll"]))
{
 $connect = mysqli_connect("localhost", "root", "", "aviation");
 $query = "SELECT * FROM student WHERE enroll = '".$_POST["enroll"]."'";
 $result = mysqli_query($connect, $query);
 if(mysqli_num_rows($result)>0)
 {
 while($row = mysqli_fetch_array($result))
 {
  $data["name"] = $row["name"];
  $data["address"] = $row["address"];
  $data["total_marks"] = $row["total_marks"];
  $data["email"] = $row["email"];
  $data["ph"] = $row["ph"];

 }

 echo json_encode($data);
}
}
?>
  • 写回答

2条回答 默认 最新

  • 谁还没个明天 2017-11-05 20:02
    关注

    you could just add

     error: function(){
      alert('No Such Data Found!');
    }
    

    after the success:function(data)

    so your ajax call would be like :

      $.ajax({
    url:"fetch.php",
    method:"POST",
    data:{enroll:enroll},
    dataType:"JSON",
    success:function(data)
    {
    if ($.trim(data)){  
     $('#employee_details').css("display", "block");
     $('#name').text(data.name);
     $('#address').text(data.address);
     $('#total_marks').text(data.total_marks);
     $('#ph').text(data.ph);
     $('#email').text(data.email);}
     else{alert('No Such Data Found!')}
    },
     error: function(){
      alert('error !');
    }
    

    or add an else in your php code that return a false if result is empty :

      if(mysql_num_rows($result)>0)
     {
    while($row = mysqli_fetch_array($result))
     {
     //code ......
    }
    
    echo json_encode($data);
    
    }else {
     return false;
     }
    

    then your ajax call would be like :

      $.ajax({
    url:"fetch.php",
    method:"POST",
    data:{enroll:enroll},
    dataType:"JSON",
    success:function(data)
    {
     if(data != false){ 
    $('#employee_details').css("display", "block");
    $('#name').text(data.name);
    $('#address').text(data.address);
    $('#total_marks').text(data.total_marks);
    $('#ph').text(data.ph);
    $('#email').text(data.email);}else{alert('No Such Data Found!');}
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog