简体   繁体   English

Ajax-将内容加载到Textarea中

[英]Ajax - Load content into Textarea

I have this simple ajax code, for some reason it wont load content onto textarea. 我有这个简单的Ajax代码,由于某种原因,它不会将内容加载到textarea上。 I need it to load the content so that it can be edited in the textarea. 我需要它来加载内容,以便可以在文本区域中对其进行编辑。

Here's the code, I'm not sure what I'm doing wrong. 这是代码,我不确定自己在做什么错。 But it displays in a div like so 但它像这样在div中显示

<div id="content"></div>

Here's the full code 这是完整的代码

<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>

Any help would be appreciated. 任何帮助,将不胜感激。

From your code this looks to do exactly what you want: 在您的代码中,这看起来完全可以满足您的要求:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
  document.getElementById("content").innerHTML=xmlhttp.responseText;
}

Since your div has the id of "content" the response is being put there. 由于您的div的ID为“ content”,因此响应被放置在那里。 Try something like: 尝试类似的方法:

document.getElementById("area1").value = xmlhttp.responseText;

I must also advise you to take a look at the PHP documentation on choosing a MySQL API as mysql_* is considered outdated and is in a long-term deprecation. 我还必须建议您查看有关选择MySQL APIPHP文档,因为mysql_*被认为已过时,并且已被长期淘汰。

这对我有用:

document.getElementById("area1").value=xmlhttp.responseText;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM