這是一個類似谷歌建議的腳本。Javascript。通過AJAX在頁面中傳遞輸入變量
我重寫了AJAX調用代碼,將其分解爲多個函數,看起來這是一個更好的跨瀏覽器/可用性方法。現在我需要將從輸入#search_text讀取的輸入變量傳遞給一個php文件,我從數據庫中實際獲取數據。
現在我只需要傳遞search_text並用echo $ _GET ['search_text']顯示它; 有人可以幫我嗎?
這裏是腳本
<script type="text/javascript">
/*note xmlHttp needs to be a global variable. Because it is not it requires that function handleStateChange to pass the xmlHttp
handleStateChange is written in such a way that is expects xmlHttp to be a global variable.*/
function startRequest(getURL){
var xmlHttp = false;
xmlHttp = createXMLHttpRequest();
//xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.onreadystatechange=function(){handleStateChange(xmlHttp);}
xmlHttp.open("GET", getURL ,true);
xmlHttp.send();
}
function createXMLHttpRequest() {
var _msxml_progid = [
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP.3.0',
'MSXML3.XMLHTTP',
'MSXML2.XMLHTTP.6.0'
];
//req is assiqning to xmlhttp through a self invoking function
var xmlHttp = (function() {
var req;
try {
req = new XMLHttpRequest();
} catch(e) {
var len = _msxml_progid.length;
while(len--) {
try {
req = new ActiveXObject(_msxml_progid[len]);
break;
} catch(e2) { }
}
} finally {
return req;
}
}());
return xmlHttp;
}
//handleStateChange is written in such a way that is expects xmlHttp to be a global variable.
function handleStateChange(xmlHttp){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
//alert(xmlHttp.status);
//alert(xmlHttp.responseText);
document.getElementById("results").innerHTML = xmlHttp.responseText;
}
}
}
function suggest() {
startRequest("ajax-submit.php");
}
</script>
<body>
<form action="" name="search" id="search">
<input type="text" name="search_text" id="search_text" onkeydown="suggest();" />
</form>
<div id="results" style="background:yellow"></div>
</body>
和PHP文件是:
<?php
echo 'Something';//'while typing it displays Something in result div
//echo $_GET['search_text'];
?>
感謝
你給了一個腳本,並尋求幫助,但你沒有指出實際上錯誤。 FWIW,我對你的建議是拋棄所有這些,並選擇任何已經爲你做到這一點的jQuery/Prototype/YUI。如果你花時間重新發明車輪,你永遠不會製造汽車。 –