我有这个简单的ajax代码,由于某种原因它不会将内容加载到textarea上。我需要它来加载内容,以便可以在文本区域中对其进行编辑。
这是代码,我不确定我做错了什么。但它显示在 div 中,如下所示
<div id="content"></div>
这是完整的代码
<html>
<head>
<title>Editing Page Content</title>
<script type="text/javascript">
function showCustomer(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("content").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("content").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","load.php?file="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<p>Page Select to edit</p>
<?php
$result = mysql_query("SELECT pagename FROM site_content");
echo "<select name=\"pagename\" onchange=\"showCustomer(this.value)\" style=\"width:300px;\">";
echo "<option selected value=''>Please select a page to edit...</option>";
while($row = mysql_fetch_assoc($result)){
echo "<option selected value='" . $row['pagename'] ."'>" . $row['pagename'] . "</option>\n";
}
echo "</select>";
?>
<div id="content">
**it works here**
</div>
<textarea cols="50" id="area1" style="position: absolute; width: 700px; height: 300px;">
**It won't work here**
</textarea>
<input type="button" value="Submit content">
</div>
</body>
</html>
如有任何帮助,我们将不胜感激。
最佳答案
从您的代码来看,这看起来完全符合您的要求:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("content").innerHTML=xmlhttp.responseText;
}
由于您的 div
的 id 为“content”,因此响应将放在那里。尝试这样的事情:
document.getElementById("area1").value = xmlhttp.responseText;
<小时/>
我还必须建议您看看PHP documentation on choosing a MySQL API因为 mysql_*
被认为已过时并且长期弃用。
关于php - Ajax - 将内容加载到 Textarea 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8843322/