我使用下面的jQuery來爲 'Live搜索' 字段檢索值:通過PHP/jQuery的/阿賈克斯檢索多個值
$(document).ready(function(){
/* LIVE SEARCH CODE - START HERE*/
var UserID = ('<?php echo $_SESSION['UserID'] ?>');
$(document).ready(function(){
$('.clLiveSearchAccount').on("keyup" , "[id*=txtAccountID]", "input", function(){
/* Get input value on change */
var inputVal = $(this).val();
var ParentTransID = $(this).prev().val();
alert(UserID);
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("Apps/Finance/incGetAccounts.php", {term: inputVal, usr: UserID }).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".clLiveSearchAccount").find('#txtAccountID').val($(this).text());
$(this).parent(".result").empty();
});
});
我使用這個PHP AJAX的處理程序:
<?php
/* ------------------------------------------------ */
$link = mysqli_connect("xxx", "xxx", "xxx", "xxx");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$term = mysqli_real_escape_string($link, $_REQUEST['term']);
$user = mysqli_real_escape_string($link, $_REQUEST['usr']);
if(isset($term)){
// Attempt select query execution
$sql = "SELECT * FROM tblAccounts WHERE Name LIKE '%" . $term . "%' AND UserID=" . $user;
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<p>" . $row['Name'] . "</p>";
}
// Close result set
mysqli_free_result($result);
} else{
echo "<p>No matches found</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
// close connection
mysqli_close($link);
?>
但是,我如何發回(並接受)一個額外的值,所以字符串名稱和整數鍵?