简体   繁体   English

使用AJAX进行Keyup上的搜索功能

[英]Search Function On Keyup With AJAX

For the first time ever I am trying to get a search function written. 我第一次尝试编写搜索功能。 I am using a AJAX function to call the php file on key up. 我正在使用AJAX函数调用键盘上的php文件。 This is performing weirdly because it is changing the content in the content area but it is not the correct content. 之所以执行异常,是因为它正在更改内容区域中的内容,但它不是正确的内容。

I am also using ajax to load data from the table to the same content area. 我还使用ajax将表中的数据加载到相同的内容区域。 My goal is that when you search it replaces the data being viewed with the most relevant data based on the criteria you have provided by that last keyup. 我的目标是,当您进行搜索时,它会根据您上次键入的关键字所提供的条件,用最相关的数据替换正在查看的数据。

This is my search form 这是我的搜索表

<div class="form-group pull-right">
                    <input type="text" name="itemID" id="itemID" class="search form-control" placeholder="Search product number">
            </div>

This is the AJAX function, 这是AJAX功能,

$("#itemID").keyup(function (){
jQuery.ajax({
          type: "GET",
          async: false,
          url: searchPath,
          data: itemID,
          cache: false,
          success: function(html) {
            $("#productResults").append(html);
        $("#productResults").html(html);    
            $('#loader_image').hide();
            if (html == "") {
             $("#loader_message").html('<p>There were no results that match your search criteria</p>').show()
            } else {
             $("#loader_message").html('Searching... Please wait <img src="http://www.wuno.com/monstroid/wp-content/uploads/2016/02/LoaderIcon.gif" alt="Loading">').show();
            }
            window.busy = false;
          }
        });
});

And this is my php search query 这是我的php搜索查询

    $sql=" SELECT * FROM wuno_inventory WHERE wuno_product like '%".$itemID."%' OR wuno_product like '%".$itemID."%'";
    try {
  $stmt = $DB_con->prepare($sql);
  $stmt->execute();
  $results = $stmt->fetchAll();
} catch (Exception $ex) {
  echo $ex->getMessage();
}
if (count($results) > 0) {
  foreach ($results as $res) {
    echo '<tr class="invent">';  
    echo '<td>' . $res['wuno_product'] . '</td>';  
    echo '<td>' . $res['wuno_alternates'] . '</td>';  
    echo '<td>' . $res['wuno_description'] . '</td>';  
    echo '<td>' . $res['wuno_onhand'] . '</td>';  
    echo '<td>' . $res['wuno_condition'] . '</td>';  
    echo '</tr>';   
  }
}

change 更改

url: searchPath,
  data: itemID,

to

url: your_php_address_file,
 data: {itemID:$(this).val()}

Change your searchpath with valid url address below 使用下面的有效网址更改您的搜索路径

 $("#itemID").keyup(function (){ var itemID = $(this).val(); var url = 'your searchpath here'; $.ajax({ type : "GET", async : false, url : url, data : "itemID=" + encodeURIComponent(itemID), cache : false, success: function(html) { $('#loader_image').hide(); $("#productResults").html(html); if (html == "") { $("#loader_message").html('<p>There were no results that match your search criteria</p>').show(); } else { $("#loader_message").html('Searching... Please wait <img src="http://www.wuno.com/monstroid/wp-content/uploads/2016/02/LoaderIcon.gif" alt="Loading">').show(); } window.busy = false; } }); }); 

Note : using queries like that may cause sqlinjection so be carefull with these sql statements '%".$itemID."%' 注意:使用类似的查询可能会导致sqlinjection,因此请小心使用这些sql语句'%“。$ itemID。”%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM