前一段時間,我做了一個「現場」搜索我的項目,所以這裏是一些代碼修改您的需求(我假設你有jQuery在您的網頁上)。
所有我建議首先你給你的投入一定的id:
<input type="text" name="PName" id="PName" />
<input type="text" name="PAddress" id="PAdress" />
<input type="text" name="PCity" id="PCity" />
在此之後,你可以綁定PNAME領域的keyup事件:
var searchTimeout; //Timer to wait a little before fetching the data
$("#PName").keyup(function() {
searchKey = this.value;
clearTimeout(searchTimeout);
searchTimeout = setTimeout(function() {
getUsers(searchKey);
}, 400); //If the key isn't pressed 400 ms, we fetch the data
});
的js函數獲取數據:
function getUsers(searchKey) {
$.ajax({
url: 'getUser.php',
type: 'POST',
dataType: 'json',
data: {value: searchKey},
success: function(data) {
if(data.status) {
$("#PAddress").val(data.userData.PAddress);
$("#PCity").val(data.userData.PCity);
} else {
// Some code to run when nothing is found
}
}
});
}
而且ofcourse的getUser.php
文件:
<?php
//... mysql connection etc.
$response = Array();
$response['status'] = false;
$query = mysql_query("SELECT `PAddress`, `PCity` FROM `Users` WHERE `PName` LIKE '%".$_POST['value']."%' LIMIT 1"); //Or you can use = instead of LIKE if you need a more strickt search
if(mysql_num_rows($query)) {
$userData = mysql_fetch_assoc($query);
$response['userData'] = $userData;
$response['status'] = true;
}
echo json_encode($response);
祝你好運! ^^
您需要了解更多關於AJAX,PHP和數據庫的信息。 – Dev
嗯,這不是我的答案? :請登錄 – jinni
如果您遇到任何問題,請嘗試自己並回來。 – Dev