使用Ajax搜索功能不起作用,不显示任何数据

时间:2018-09-24 13:37:15

标签: php jquery ajax

我已经在网页中实现了搜索功能选项。但是问题是无法显示空白页的数据。我在控制台中检查了没有错误。在控制台中检查了它们没有错误。

这是该代码

<html>
<head>
<script src="assets/js/jquery.js"></script>
 <script src="assets/js/bootstrap.js"></script>
<link href="assets/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
   <div class="container">
   <br/>
    <div class="form-group">
    <div class="input-group">
             <span class="input-group-addon">Search</span>
          <input type="text" name="search_text"  id="search_text" class="form_control"/>
          </div>
          </div>
          <br/>
          <div id="result"></div>
    </div>
    </body>
    </html>
     <script type="text/javascript">
     $(document).ready(function(){  
     $('#search_text').keyup(function(){
    var txt=$(this).val();
    if(txt!='')
    {

    }
    else
    {
        $('#result').html('');
        $.ajax({                
        url:"searchpatient.php",
        method:"post",
        data:{search:txt},  
        dataType:"text",
        success:function(data)
        {   
            $('#result').html(data);
        }               
        });     
        }
     });
     });
     </script>

Searchpatient.php

<?php 
$connect = mysqli_connect("localhost","root","","learn");
$output =  '';
    $sql="SELECT * FROM appointment WHERE status='1' AND first_name LIKE '%".$_POST["search"]."%'";     
    $result=mysqli_query($connect,$sql);
    if(mysqli_num_rows($result)>0)
    {           
        $output .='<h2>Patient Record</h2>';
        $output .='<div class="table-responsive">
                      <table class="table table-striped">
                        <thead>
                          <tr>                               
                            <th>Name</th>
                            <th>Gender</th>
                            <th>Department</th>
                            <th>Phonenumber</th>
                            <th>Date</th>
                            <th>Address</th>
                          </tr>
                        </thead>';
                        while ($row=mysqli_fetch_array($result))                                
                        $output .='<tr>
                                        <td>' . $row['first_name'].' </td>
                                        <td>' . $row['gender'].'</td>
                                        <td>' . $row['department'].'</td>
                                        <td>' . echo $row['phone_no'].'</td>                                            
                                        <td>' . echo $row['date'].'</td>                                            
                                        <td>' . echo $row['address'].'</td>                                           
                                    </tr>';                         
    }
    echo $output;
else{
    echo 'Data Not Found';
    }
  ?>

我去过youtube上的一本教程,并遵循了该教程,但是他们的代码运行良好,但对我来说却遇到了问题。希望有人能找到解决方案。在此先感谢您

1 个答案:

答案 0 :(得分:0)

<html>
<head>
<script src="assets/js/jquery.js"></script>
<script src="assets/js/bootstrap.js"></script>
<link href="assets/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br/>
    <div class="form-group">
    <div class="input-group">
             <span class="input-group-addon">Search</span>
          <input type="text" name="search_text"  id="search_text" class="form_control"/>
          </div>
          </div>
          <br/>
          <div id="result"></div>
    </div>
    </body>
    </html>
 <script type="text/javascript">
 $(document).ready(function(){
 $('#search_text').keyup(function(){
 var txt=$(this).val();
 if(txt!='')
 {
$('#result').html('');
$.ajax({
url:"searchpatient.php",
       method:"post",
       data:{search:txt},
dataType:"text",
success:function(data)
{
$('#result').html(data);
}
});
}
});
});
</script>

searchpatient.php

<?php 
$connect = mysqli_connect("localhost","root","","learn");
$output =  '';
    $sql="SELECT * FROM appointment WHERE status='1' AND first_name LIKE '%".$_POST["search"]."%'";     
    $result=mysqli_query($connect,$sql);
    if(mysqli_num_rows($result)>0)
    {           
        $output .='<h2>Patient Record</h2>';
        $output .='<div class="table-responsive">
                      <table class="table table-striped">
                        <thead>
                          <tr>                               
                            <th>Name</th>
                            <th>Gender</th>
                            <th>Department</th>
                            <th>Phonenumber</th>
                            <th>Date</th>
                            <th>Address</th>
                          </tr>
                        </thead>';

                        while ($row=mysqli_fetch_array($result))                                
                        $output .='<tr>
                                        <td>' . $row['first_name'].' </td>
                                        <td>' . $row['gender'].'</td>
                                        <td>' . $row['department'].'</td>
                                        <td>' . $row['phone_no'].'</td>                                         
                                        <td>' . $row['date'].'</td>                                         
                                        <td>' . $row['address'].'</td>                                           
                                    </tr>';     
        echo $output;
    }       
else{
    echo 'Data Not Found';
    }
    ?>