我有一些复杂的查询。当返回AJAX查询结果时,当我在所选项目上选中复选框时,我希望删除其他选项。原因是因为目前我必须输入整个匹配结果,因此结果缩小为一个项目。否则,用户必须检查他们选择的项目,然后向下滚动大约一半的页面以单击提交按钮以在不同的表格上添加他们选择的项目。
到目前为止,这是我的代码;
trigger.js
// SKU search AJAX START //
$(document).ready(function() {
$(".tablesearch").hide();
// Search
function search() {
var query_value = $('input#barcode').val();
if(query_value !== ''){
$.ajax({
type: "POST",
url: "ajax/inventory.php",
data: { query: query_value },
cache: false,
success: function(html){
$("table#resultTable tbody").html(html);
}
});
}return false;
}
$("input#barcode").on("keyup", function(e) {
// Set Timeout
clearTimeout($.data(this, 'timer'));
// Set Search String
var search_string = $(this).val();
// Do Search
if (search_string == '') {
$(".tablesearch").fadeOut(300);
}else{
$(".tablesearch").fadeIn(300);
$(this).data('timer', setTimeout(search, 100));
};
});
});
// SKU search AJAX END //
DB-ajax.php
<?php
// Credentials
$dbhost = "localhost";
$dbname = "cms";
$dbuser = "root";
$dbpass = "pass";
// Connection
global $test_db;
$ajax_db = new mysqli();
$ajax_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$ajax_db->set_charset("utf8");
// Check Connection
if ($ajax_db->connect_errno) {
printf("Connect failed: %s\n", $ajax_db->connect_error);
exit();
}
?>
inventory.php
<?php
require_once "db-ajax.php";
// Output HTML formats
$html = "<tr>";
$html .= "<td>skuString</td>";
$html .= "<td>catString</td>";
$html .= "<td>descString</td>";
$html .= "<td>stockString</td>";
$html .= "<td>priceString</td>";
$html .= "<td><input class='checkBoxes' type='checkbox' name='checkBoxArray[]' value='skuIdString'></td>";
$html .= "</tr>";
// Get the Search
$search_string = preg_replace("/[^,-\/\sA-Za-z0-9_]/", " ", $_POST['query']);
$search_string = $ajax_db->real_escape_string($search_string);
// Check if length is more than 1 character
if (strlen($search_string) >= 1 && $search_string !== ' ') {
$query = 'SELECT s_id, sku_number, category, description, price, in_stock ';
$query .= 'FROM parts_stock AS a JOIN items AS b ON a.stock_id = b.s_id ';
$query .= 'WHERE sku_number LIKE "%'.$search_string.'%" OR category LIKE "%'.$search_string.'%"';
// Do the search
$result = $ajax_db->query($query);
while($results = $result->fetch_array()) {
$result_array[] = $results;
}
// Check for results
if (isset($result_array)) {
foreach ($result_array as $result) {
// Output strings and highlight the matches
$d_sku = preg_replace("/".$search_string."/i", "<b>".$search_string."</b>", $result['sku_number']);
$d_cat = $result['category'];
$d_desc = $result['description'];
$d_stock = $result['in_stock'];
$d_price = $result['price'];
$s_id = $result['s_id'];
// Replace the items into above HTML
$o = str_replace('skuString', $d_sku, $html);
$o = str_replace('catString', $d_cat, $o);
$o = str_replace('descString', $d_desc, $o);
$o = str_replace('stockString', $d_stock, $o);
$o = str_replace('priceString', $d_price, $o);
$o = str_replace('skuIdString', $s_id, $o);
// Output it
echo($o);
}
}else{
// Replace for no results
$o = str_replace('skuString', '<span class="label label-danger">No SKUs Found</span>', $html);
$o = str_replace('catString', '', $o);
$o = str_replace('descString', '', $o);
$o = str_replace('stockString', '', $o);
$o = str_replace('priceString', '', $o);
$o = str_replace('skuIdString', '', $o);
// Output
echo($o);
}
}
?>