0
我正在使用一個簡單的「實時搜索」腳本,在用戶鍵入文本框時顯示來自MySQL數據庫的結果。如果Javascript指向一個完全獨立的頁面,但它需要它指向同一頁面,它會很好地工作。不幸的是,當我嘗試並做到這一點時,頁面會在生成結果時被複制。如何停止xmlhttp.open複製整個頁面? (ajax livesearch)
這按預期工作:
Document called: "test.php" containing JavaScript below and test2.php containing the PHP code
xmlhttp.open("GET","test2.php?livesearch="+str,true);
xmlhttp.send();
這將創建一個頁面一個頁面內:
Document called: "test.php" containing both the JavaScript and PHP code below
xmlhttp.open("GET","?livesearch="+str,true);
xmlhttp.send();
據我所知,這是因爲它是在一個循環中打開自己,但我不知道是什麼我應該改變代碼以避免這種情況。任何幫助將不勝感激,因爲我沒有找到通過谷歌很多的幫助。
這裏是我的所有代碼:
的Javascript
function showResult(str)
{
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("livesearch").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","&livesearch="+str,true);
xmlhttp.send();
}
PHP代碼
if(isset($_GET['livesearch'])) {liveSearch();}
function liveSearch() {
$q=$_GET["livesearch"];
$sqlQuery = "SELECT * FROM something WHERE something LIKE '%" . $q . "%' ;
etc etc etc
}
我不認爲你有任何例子,或可以指出我在正確的方向上如何使用正則表達式與上述? – David 2011-02-14 19:48:19