【问题标题】:Jquery, ajax, PHP - Live search not returning values in some situationsJquery、ajax、PHP - 在某些情况下实时搜索不返回值
【发布时间】:2016-07-28 12:50:29
【问题描述】:

我正在实时搜索医疗卡号码。它与 MySQL 数据库链接,并且在大多数情况下它可以工作......有点。我遇到了一个问题,其中某些数字字符串似乎会给它带来麻烦的字符串,例如 12345689999(我的七键坏了)。它似乎是在一个又一个重复使用相同的字符时出现的。我的 HCN 数据库中的数据类型是文本。我的代码如下:

HTML

<div class="container-fluid">

         <div class="jumbotron">

             <h1 class="display-3">Search Patient Using Health Card Number</h1>
             <hr class="m-y-2">

             <p class="lead">

                 <input type="text" class="form-control" id="hcnSearch" aria-describedby="hcn" placeholder="Search health card number">

             </p>

        </div>

        <div id="result">

        </div>

    </div>

Jquery / AJAX

$(document).ready(function(){

            $("#hcnSearch").keyup(function() {

                var txt =$(this).val();

                if (txt != '') {

                    $('#result').html('');

                    $.ajax({
                        url:"fetch2.php",
                        method: "post",
                        data:{search2:txt},
                        dataType:"text",
                        success:function(data)
                        {
                            $('#result').html(data);
                        }
                    });


                } else {

                    $('#result').html('');
                }

            });
        });

在我的 fetch2.php 文件中

<?php
session_start();

include("connection.php");

$output = '';

$sql = "SELECT * FROM PATIENT_ID_DEMO WHERE H_CARD_NUMBER LIKE '%".mysqli_real_escape_string($link, $_POST["search2"])."%'";

$result = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {


  while ($row = mysqli_fetch_array($result)) {

      if ($row[5] == 1) {
          $row5 = "Yes";
      } else {
          $row5 = "No";
      }

      if ($row[8] == "M") {
          $row8 = "Male";
      } else {
          $row8 = "Female";
     }

      $output .= '<h4 align="center">Search Result</h4>';  
      $output .= '<div class="table-responsive">  
                    <table class="table table bordered">  
                        <tr>  
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Middle Name</th>
                            <th>DOB</th>
                            <th>NS health card</th>
                            <th>Health card number</th>
                            <th>Gender</th>
                            <th>Age</th>
                            <th>UID</th>
                        </tr>';  

  while($row = mysqli_fetch_array($result))  
  {  
       $output .= '  
            <tr>  
                <td>'.$row[1].'</td>
                <td>'.$row[2].'</td>
                <td>'.$row[3].'</td>
                <td>'.$row[4].'</td>
                <td>'.$row5.'</td>
                <td>'.$row[7].'</td>
                <td>'.$row8.'</td>
                <td>'.$row[9].'</td>
                <td>'.$row[10].'</td>
            </tr>  
       ';  
  }  
  echo $output;

}

} else {

   echo "No data found...";

}


?>

此代码允许我输入(搜索)12345689,查询将显示一些相关的健康卡号码,但是如果我正在寻找 12345689999 并输入第二个 9...所有搜索都会消失。

【问题讨论】:

  • for1234568999 是否有 dB 值
  • 是的,这个值甚至会出现,然后在输入第二个九后消失@KARTHISRV

标签: php jquery mysql ajax livesearch


【解决方案1】:

将方法改为GET

$.ajax({
         url:"fetch2.php",
         method: "GET",
         data:{search2:txt},
         dataType:"text",
         success:function(data)
         {
           $('#result').html(data);
         }
      });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2010-12-31
    • 2016-09-08
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多