我想用ajax,我曾經使用過它,複製我以前的HTML文件中的代碼,並改變了價值觀發送,PHP文件來處理的代碼和我」 m使用POST方法。但是,php腳本不會返回任何內容到html頁面。我已經檢查了所有可以,所以我現在在這裏。 var x = document.getElementsByTagName('option')[document.getElementById(「region」)。selectedIndex] .value; var ajaxRequest; //使Ajax成爲可能的變量!PHP的任何結果,不返回到JavaScript的Ajax對象
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// modal section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById("accordion");
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
ajaxRequest.open("POST", "search.php", true);
ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
alert(x);
ajaxRequest.send("region=" + x);
}
PHP代碼是一個簡單的
<?php
echo "The world is blue.";
?>
的是,它沒有返回到所選擇的股利。
首先,我建議使用像jQuery這樣的框架,它使AJAX變得更容易。其次,你確定你從PHP獲得結果嗎? – cgf
這就是問題所在,我沒有從php獲得任何結果。我有另一個適用於此的應用程序,但是,這個應用程序不會將任何內容返回給id爲「accordion」的div。 – Michael
@Michael你的PHP是原始的,必須工作。嘗試使用您的JavaScript代碼的螢火蟲。 – user4035