2012-03-25 62 views
0

我正在開發一個使用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 
} 
?> 

回答

0

有我的解決方案爲您提供:

只需添加以下行這樣的:

<script type="text/javascript"> 

var keyword = ''; // add this! 
var city = ''; // add this! 
var state = ''; // add this! 

function showHint(keyword, city, state) { 
// etc. 

編碼愉快!

+0

它的工作原理,非常感謝您的幫助! – 2012-03-25 23:42:52

0

你應該考慮動態建立查詢要佔你的領域可用。下面是一些我剛輸入的代碼,作爲概念。我沒有測試過,也不能保證它是完美的。

<?php 

    // Get the parameters from URL 
    $params['keyword'] = $_GET['keyword']; 
    $params['city'] = $_GET['city']; 
    $params['state'] = $_GET['state']; 

    // Start building query 
    $sql = "SELECT * FROM users WHERE ";  

    // Loop through GET params and create WHERE clause 
    foreach($params as $key=>$value) 
    { 
     // Only add to query if parameter not empty 
     if(!empty(trim($value))) 
     { 
     // Add to an array to be joined later using "AND" 
     switch ($key) { 
      case 'keyword': 
       $whereCond[] = "ClinicName LIKE '%$value%'"; 
       break; 
      case 'city': 
       $whereCond[] = "LocationCity LIKE '%$value%'"; 
       break; 
      case 'state': 
       $whereCond[] = "LocationRegion LIKE '%$value%'"; 
       break; 
     } 
     } 
    } 

    // With where conditions, use a counter so 
    // we dont add "AND" to last one 
    $iCount = 0; 
    $iTotal = count($whereCond); 

    // Combine WHERE clause and add to query 
    foreach($whereCond as $cond) 
    { 
     if($iCount != $iTotal) 
     { 
     $sql .= $cond . " AND "; 
     } else { 
     $sql .= $cond; 
     } 

     // Increment counter 
     $iCount++; 
    } 

    // Run query 
    $query = mysql_query($sql) or die(mysql_error());