2011-09-01 54 views
0

我在使用AJAX從我的代碼獲取結果時沒有得到響應。同時發送參數到文件我的代碼工作正常...即發送參數到文件,因爲我已經使用警報消息檢查.. 但我的PHP文件沒有顯示任何響應。使用ajax獲取響應時出現問題

AJAX CODE。

function viewsg(){ 

document.getElementById('sgwaitingmsg').innerHTML = "Just a second..."; 
var childid = encodeURIComponent(document.getElementById('childsgselect').value); 
var parameters ='childid=' + childid; 
http.open('POST', 'fetchsg.php', true); 
http.send(parameters); 
//alert(parameters); 
http.onreadystatechange = function(){ 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("sgwaitingmsg").innerHTML=xmlhttp.responseText; 
     } 
    } 
} 

PHP代碼。

<?php 
//$_REQUEST["childid"]; 
//$_REQUEST["nocache"]; 
echo $_REQUEST["childid"]; 
?> 

HTML代碼。

<label id="sgwaitingmsg"></label> 
<select id="childsgselect" onchange="viewsg()" > 
<option value="">Select Child</option> 
<?php while($result=mysql_fetch_assoc($child)){print '<option value="'.$result["snum"].'">'.$result["sfname"].'</option>'; }?> 
</select> 

回答

1

$_REQUEST["childid"];什麼也沒做。嘗試

echo $_POST["childid"]; 

代替

,你有語法錯誤在你的代碼

if (xmlhttp.readyState==4 &&) 

應該

if (xmlhttp.readyState==4) 

試試這個http://sandbox.phpcode.eu/g/904ed/10

<?php 
if (isset($_POST['childid'])){ 
    echo $_POST['childid']; 
    die(); 
} 
?> 
<div id="sgwaitingmsg"></div> 
<input id="childsgselect" value="hellllo" /> 
<script> 
function viewsg(){ 

document.getElementById('sgwaitingmsg').innerHTML = "Just a second..."; 
var childid = encodeURIComponent(document.getElementById('childsgselect').value); 
var parameters ='childid=' + childid; 
var params ='childid=' + childid; 
http=new XMLHttpRequest(); 
http.open('POST', '<?php echo $_SERVER['PHP_SELF']; ?>', true); 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 
http.send(parameters); 
//alert(parameters); 
http.onreadystatechange = function(){ 
     if (http.readyState==4) 
    { 
    document.getElementById("sgwaitingmsg").innerHTML=http.responseText; 
     } 
    } 
} 
viewsg(); 
</script> 

是完全正常工作的腳本。你將不得不通過

+0

它同樣,沒有獲取響應文本。 –

+0

@ user921300:你的JS有sytnax錯誤。看看我的編輯 – genesis

+0

@那我已經試過了'if(xmlhttp.readyState == 4 && xmlhttp.status == 200)'。但那也行不通。 –