我正在開發一個使用Ajax和PHP的實時搜索系統,它運行良好。雖然,我有一個問題,我可以說它正在工作,因爲它應該。我有多個文本字段,然後輸入列表更新的信息。 但是,只有在每個字段中輸入了內容時,列表纔會開始更新。有沒有辦法來解決這個問題?雖然這個工作正常,但我確實想添加更多的字段。用多個值進行Ajax實時搜索時遇到問題(PHP)
阿賈克斯:
<script type="text/javascript">
function showHint(keyword, city, state) {
var xmlhttp;
if(keyword.length == 0 & city.length == 0 & state.length == 0) {
document.getElementById("txtHint").innerHTML = "";
}
if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "gethint.php?keyword=" + keyword + "&city=" + city + "&state=" + state, true);
xmlhttp.send();
}
</script>
HTML表單:
<form action="" id="livesearch" id="livesearch">
Clinic Name: <input type="text" id="clinicsearch" name="clinicsearch" autocomplete="off" onkeyup="keyword=this.value; showHint(keyword, city, state);" />
City: <input type="text" id="citysearch" name="citysearch" autocomplete="off" onkeyup="city=this.value; showHint(keyword, city, state);" />
State: <input type="text" id="statesearch" name="statesearch" autocomplete="off" onkeyup="state=this.value; showHint(keyword, city, state);" />
</form>
PHP腳本:
<?php
//get the parameters from URL
$keyword = $_GET['keyword'];
$city = $_GET['city'];
$state = $_GET['state'];
$query = mysql_query("SELECT * FROM users WHERE ClinicName LIKE '%$keyword%' AND LocationCity LIKE '%$city%' AND LocationRegion LIKE '%$state%'") or die(mysql_error());
if($query){//If query successfull
if(mysql_affected_rows()!=0){//and if atleast one record is found
while($rows = mysql_fetch_array($query)){ //Display the record
$replace = str_replace(" ", "-", $rows['ClinicName']);
echo '<p>'.$rows['UserID'] .' - <a href="clinic.php?clinicname='.$replace.'">'.$rows['ClinicName'].'</a> - '.$rows['Phone1'].'-'.$rows['Phone2'].'-'.$rows['Phone3'].' - '.$rows['LocationCity'].', '.$rows['LocationRegion'].' '.$rows['LocationZip'].', '.$rows['LocationCountry'].'</p>';
}
}
else {
echo 'No Results for:<br />Clinic Name: '.$keyword.'<br />City: '.$city.'<br />State: '.$state.'';//No Match found in the Database
}
}
else {
echo 'Parameter Missing in the URL';//If URL is invalid
}
?>
它的工作原理,非常感謝您的幫助! – 2012-03-25 23:42:52