2

这个 ajax 测试应该(现在大约 2 小时)将请求结果返回到 textarea。请求是针对同一页面发出的,我在正文顶部有一个 $_POST isset 测试,以检查请求是否来自我的 POST 请求(我需要将代码全部放在一个文件中)。结果是“出现在 textarea 框中的文本”自行返回,而不是放在 textarea 内。

//name of this page is testing.php  

<html>
<head>
function loadXMLDoc()
{
var xmlhttp;

xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("testTextarea").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.open("POST","testing.php",true);
xmlhttp.send("test");
}

</head>

<?
if (isset($_POST["testName"])) {
die("text to appear in the textarea box");
}
?>

<body>


<form action="testing.php" method="POST" onsubmit="loadXMLDoc(this.form); return false;">
<input class="command" type="text" name="testName" />
<div><textarea id="testTextarea"></textarea></div>


</body>
</html>
4

1 回答 1

2

尝试将 die() 移到顶部,以便在 die() 之前不输出任何其他内容

<?
if (isset($_POST["testName"])) {
die("text to appear in the textarea box");
}
?>

<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;

xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("testTextarea").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","testing.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("testName=blah");
}
</script>

</head>

<body>


<form action="testing.php" method="POST" onsubmit="loadXMLDoc(this.form); return false;">
<input class="command" type="text" name="testName" />
<div><textarea id="testTextarea"></textarea></div>
</form>

</body>
</html>
于 2013-05-31T20:54:25.517 回答