使用AJAX从我的代码中获取结果时,我没有得到响应。在向文件发送参数时,我的代码运行良好...即将参数发送到文件,因为我已经使用alert msg检查了.. 但是我的php文件没有显示任何响应。
AJAX代码。
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 CODE。
<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>
答案 0 :(得分: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>
是完全可用的脚本。你必须经历