2013-01-11 54 views
0

我在HTML
Live Search的工作,我工作得很好,但我得到了一個小問題, 這裏是我的指數代碼:清晰的列表中向下的HTML

<form id="quick-search" action="livesearch.php" method="GET" > 
<p> 
Search: 
<input id="qsearch" type="text" name="qsearch" onkeyup="liveSearch()" /> 
<input type="submit" /> 
</p> 
<div id="searchResults"> 

</div> 
</form> 

這裏是我的js代碼:

function liveSearch() 
{ 
    var url = "livesearch.php"; 
    var s = document.getElementById('qsearch').value; 
    http.open("POST", "livesearch.php?qsearch="+s, true); 
    http.onreadystatechange = function() 
    { 
     if(http.readyState == 4 && http.status == 200) 
     { 
      document.getElementById('searchResults').innerHTML = 'Suggestions are as follow'+http.responseText; 
      //alert(http.responseText); 
     } 
    } 

    http.send(); 
} 

我正確地得到結果,但是,當我清空完整的輸入框,然後我正在從數據庫中完整列表框中,在空的輸入框,我想要清除列表框

+0

是問題缺失的最後一部分? –

+0

對不起,請... –

+0

有沒有我們可以查看當前版本的版本? – Ladineko

回答

0

你應該保護你的代碼在PHP和用戶端。要做到這一點檢查用戶確實發送了多少個字母:if(s.length < 2) return;防止AJAX請求

function liveSearch() 
    { 
    var url = "livesearch.php"; 
    var s = document.getElementById('qsearch').value; 
    if(s.length < 2) return; // here You escape if there isn't enough letters to search 
    http.open("POST", "livesearch.php?qsearch="+s, true); 
    http.onreadystatechange = function() 
    { 
     if(http.readyState == 4 && http.status == 200) 
     { 
      document.getElementById('searchResults').innerHTML = 'Suggestions are as follow'+http.responseText; 
      //alert(http.responseText); 
     } 
    } 

    http.send(); 
} 

但要記住,以確保它在PHP以及:

if(count($_REQUEST['qsearch']) < 2) return false; 
+0

thanx bumerang .. –

+1

始終在您的服務;) – bumerang