2011-03-17 40 views
0

我有這個實時搜索工作完美,直到我的MySQL數據庫中的公司名稱中有一個apostrophie。一個例子 - 廁所的起來。 即時搜索使用的是從php5開發它的人。我已經設法修改代碼來處理我的代碼,但是在從列表中選擇名稱時出現失敗,但是失敗了。當我選擇名稱時,輸入框留空。實時搜索不能與apostrophies一起使用php/javascript

<div> 
    <? print"<form method=\"post\" action=ordernew.php?action=startneworder>"; ?> 
     <div> 
      Customer Name: 
      <br /> 
      <input type="text" size="30" name="company" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" /> 
     </div> 

     <div class="suggestionsBox" id="suggestions" style="display: none;"> 
      <img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" /> 
      <div class="suggestionList" id="autoSuggestionsList"> 
       &nbsp; 
      </div> 
     </div> 
     <? print"<input type=submit value=Continue />"; ?> 
    </form> 
</div> 

上面的代碼是我的表單上的代碼。下面的代碼是所有的工作。

<?php 
require_once("functions.php"); 

// dbconn(false); // loggedinonly();
// PHP5實現 - 使用MySQLi。 // mysqli('localhost','yourUsername','yourPassword','yourDatabase'); $ db = new mysqli($ mysql_host,$ mysql_user,$ mysql_pass,$ mysql_db);

if(!$db) { 
    // Show error if we cannot connect. 
    echo 'ERROR: Could not connect to the database.'; 
} else { 
    // Is there a posted query string? 
    if(isset($_POST['queryString'])) { 
     $queryString = $db->real_escape_string($_POST['queryString']); 

     // Is the string length greater than 0? 

     if(strlen($queryString) >0) { 
      // Run the query: We use LIKE '$queryString%' 
      // The percentage sign is a wild-card, in my example of countries it works like this... 
      // $queryString = 'Uni'; 
      // Returned data = 'United States, United Kindom'; 

      // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE. 
      // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10 

      $query = $db->query("SELECT company FROM eventcompany WHERE company LIKE '$queryString%' LIMIT 10"); 
      if($query) { 
       // While there are results loop through them - fetching an Object (i like PHP5 btw!). 
       while ($result = $query ->fetch_object()) { 
        // Format the results, im using <li> for the list, you can change it. 
        // The onClick function fills the textbox with the result. 

        // YOU MUST CHANGE: $result->value to $result->your_colum 
        echo '<li onClick="fill(\''.$result->company.'\');">'.$result->company.'</li>'; 
        //echo '<li onClick="fill(\"'.$result->company.'\");">'.$result->company.\"</li>'; 
       } 
      } else { 
       echo 'ERROR: There was a problem with the query.'; 
      } 
     } else { 
      // Dont do anything. 
     } // There is a queryString. 
    } else { 
     echo 'There should be no direct access to this script!'; 
    } 
}?> 

這部分是在我的頭部。

<script type="text/javascript"> 
function lookup(inputString) { 
    if(inputString.length == 0) { 
     // Hide the suggestion box. 
     $('#suggestions').hide(); 
    } else { 
     $.post("backend/rpc.php", {queryString: ""+inputString+""}, function(data){ 
      if(data.length >0) { 
       $('#suggestions').show(); 
       $('#autoSuggestionsList').html(data); 
      } 
     }); 
    } 
} // lookup 

function fill(thisValue) { 
    $('#inputString').val(thisValue); 
    setTimeout("$('#suggestions').hide();", 200); 
} 

我認爲問題出在上單擊事件。它適用於&個字符,但不適用於'。 歡迎任何建議。 許多感謝

史蒂夫

我試圖以顯示圖像,但我低代表點。 感謝您的response.i現在得到這個廁所的顯示,但是當我選擇選擇輸入框仍然是空白的。這隻發生在與他們在一起的公司。我知道沒有多少公司在其中,但現在更多或者是挑戰。

排序你幾乎在那裏。

$company = addslashes($result->company); 
    echo "<li onClick=\"fill('".$company."');\">".$result->company."</li>"; 

藉助螢火蟲。非常感謝我注意到第一列需要addslashes和第二個沒有。所以我想你可能剛剛弄錯了他們)感謝你的幫助。

史蒂夫

+0

有什麼錯誤訊息?它來自PHP還是Javascript? – Cfreak 2011-03-17 16:01:37

+0

沒有錯誤信息。在我的表單的輸入框中留下空白,當我的值有一個apostrophie在它。如果該值是一個正常的單詞,它工作正常。 – 2011-03-17 16:20:48

回答

2

答案是,額外的報價是搞亂你的HTML。

使用的 「廁所的」 你的公司例如,該行:

echo '<li onClick="fill(\''.$result->company.'\');">'.$result->company.'</li>'; 

將打印HTML:

<li onClick="fill('loo's');">loo's</li>'; 

問題是與額外的單引號的位置:

onClick="fill('loo's');" 

需要什麼,而不是打印是:

onClick="fill('loo\'s');" 

解決它的最簡單的方法是將PHP更改爲:

$company = addslashes($result->company); 
echo "<li onClick=\"fill('".$result->company."');\">".$company."</li>"; 

我沒有測試代碼來驗證,但應該可以解決這個問題。

另外,嘗試在Firefox中運行的頁面。安裝Firebug插件。您可以在Firebug的控制檯選項卡中查看通過Ajax返回的內容。這將在未來幫助你。

+0

我試圖以顯示圖像,但我有低代表點。 感謝您的response.i現在得到這個廁所的顯示,但是當我選擇選擇輸入框仍然是空白的。這隻發生在與他們在一起的公司。我知道沒有多少公司在其中,但現在更多或者是挑戰。 – 2011-03-17 17:32:28

+0

我安裝的Firefox螢火蟲,它給我以後的參數列表這個錯誤遺漏) http://www.website.co.uk/dir/new.php 1號線填充(「廁所的公司」); – 2011-03-17 17:45:05

相關問題