简体   繁体   English

Javascript AJAX请求未返回HTML表单文本区域

[英]Javascript AJAX request is not returning a HTML form textarea

I'm trying to have my page show a HTML textarea submission form after the user clicks on a button using AJAX, but for some reason the response from AJAX shows everything except for the textarea. 我试图让用户使用AJAX单击按钮后,页面显示HTML文本区域提交表单,但是由于某种原因,AJAX的响应会显示除文本区域之外的所有内容。 I'm a bit new to web developing so my code is a mess. 我对Web开发有点陌生,所以我的代码一团糟。 I deleted some stuff out of the code to make it easier to read, but the code below is still not working. 我从代码中删除了一些东西,以使其更易于阅读,但是下面的代码仍然无法正常工作。 The javascript part is: JavaScript部分是:

var time_variable;

function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object
var poll_id;

function getVote(choice, id, poll, display) {
  switch(poll)
  {
  case 1:
    poll_id = '1';
    break;
  case 2:
    poll_id = '2';
    break;
  case 3:
    poll_id = '3';
    break;
  default:
    alert("Error in poll ID..");
  }

  var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
    //var txtname = document.getElementById("txtname");
    xmlhttp.open("POST","poll_vote.php",true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("choice=" + choice + "&id=" + id + "&disp=" + display); //Posting txtname to PHP File
  }
}


function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("poll"+poll_id).innerHTML=xmlhttp.responseText; //Update the HTML Form element 
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

The buttons to click are: 要单击的按钮是:

<div id="poll1">
            <table id="pollm"><tr><td>
            <a href="#" onclick="getVote(0,<?php echo $id; ?>,1,'m')" ><img src="/Images/A_not_pressed_small.png" border="0" onmouseover="this.src='/Images/A_pressed_small.png';" onmouseout="this.src='/Images/A_not_pressed_small.png';" /></a>
            </td><td>
            <?php echo $current['choice1']; ?>
            </td></tr>
            </table>
            <table id="pollm"><tr><td>  
            <a href="#" onclick="getVote(1,<?php echo $id; ?>,1,'m')" ><img src="/Images/B_not_pressed_small.png" border="0" onmouseover="this.src='/Images/B_pressed_small.png';" onmouseout="this.src='/Images/B_not_pressed_small.png';" /></a>
            </td><td>
            <?php echo $current['choice2']; ?>
            </td></tr>
            </table>
            </div>

And poll_vote.php is just a table: 而且poll_vote.php只是一个表:

<table width="200" border="0" cellpadding="0" cellspacing="0" >
<tr>
<form name="form1" method="post" action="add_comment.php">
<td><textarea name="a_answer" cols="45" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Submit" class="text_submit"></td>
<td>hello world!</td>
<td></td>

</tr>
</table></td></tr>
    </table>

After I click the button, I can see the submit button and "hello world!" 单击按钮后,可以看到“提交”按钮和“ hello world!”。 AJAX response, but no textarea. AJAX响应,但没有文本区域。 Anyone have any suggestions please? 有人有什么建议吗? Thanks 谢谢

It might be worth you learning jQuery. 您可能值得学习jQuery。 This will save you time in the future. 这样可以节省您的时间。

I agree with one comment that putting the form tag before the <td> is odd, but why fix your logic there. 我同意一条评论,即将form标记放在<td>之前很奇怪,但是为什么要在那里修改逻辑。

One problem your browser may have is you are missing a close element for <form> : 您的浏览器可能存在的一个问题是您缺少<form>的close元素:

<tr>
<form name="form1" method="post" action="add_comment.php">
<td><textarea name="a_answer" cols="45" rows="3" id="comment"></textarea></td>
</tr>

<tr>
<form name="form1" method="post" action="add_comment.php">
<td><textarea name="a_answer" cols="45" rows="3" id="comment"></textarea></td>
</form>
</tr>

If you use firebug, as suggested, or the IE debugger on IE8, you can look at what is actually returned from the server, and then you can see what is going on, but it is hard to troubleshoot this type of problem without doing the server and client side. 如果按照建议使用firebug或IE8上的IE调试器,则可以查看服务器实际返回的内容,然后可以查看发生了什么,但是如果不这样做,很难解决此类问题。服务器和客户端。

It will be worth your time to learn to use the debuggers for the browsers you want to test with, otherwise javascript becomes very frustrating. 值得您花时间学习如何在要测试的浏览器上使用调试器,否则javascript会非常令人沮丧。

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

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