0

the current xmlhttp function i am using is passing a very tiny int to the php file for processing using the GET method.

xmlhttp=GetXmlHttpObject();
        if (xmlhttp==null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }
        var url="user_submit.php";
        url=url+"?vote="+value;
        url=url+"&sid="+Math.random();
        xmlhttp.onreadystatechange=stateChanged;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
        function stateChanged()
        {
          if (xmlhttp.readyState==4)
          {
          document.getElementById("captionbox").innerHTML=xmlhttp.responseText;
          }
        }

now i want to process a comment box in this ajax request, which i suppose would require a POST call? can anyone help me with this?

i need to send the contents of the comment box through this ajax request to the php file for processing and then adding into the DB.

2 Answers 2

2
...
var url="user_submit.php",
    data="vote="+value+"&sid="+Math.random();
xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.setRequestHeader("Content-length", data.length);
xmlhttp.open("POST",url,true);
...
xmlHttp.send( data );
...
2
  • the above code when added raises this error in Firebug. uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.setRequestHeader]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: localhost/roadies/JS/script.js :: anonymous :: line 91" data: no]
    – amit
    Dec 21, 2009 at 20:40
  • you have to do more than copy paste it, you have to change the ... to what your code is
    – Dan Beam
    Dec 22, 2009 at 6:55
1

You use send() to send POST data this way. You then have to send the right headers and encode that data correctly, etc.

That being said, it's incredibly tedious using XmlHttpRequest. There are cross-browser issues to consider (eg fallbacks for when XmlHttpRequest doesn't exist) and as you can see the code itself is messy.

Consider using a Javascript library like jQuery instead and then it reduces your code to:

$("#captionbox").load('user_submit.php', {
  vote: value,
  sid: 12345
});

which will also do the encoding of query string parameters (and many other things) correctly out of the box.

To post a comment, imagine you have:

<textarea id="comment"></textarea>
<input type="button" id="send" value="Send">

then:

$.post("post_comment.php", {
  comment: $("#comment").text()
});

And jQuery is only 19K minified and gzipped.

5
  • I'd also agree with cletus, use a library if you don't feel like drudging up your good ol' AJAX functions (like I just did for you)
    – Dan Beam
    Dec 21, 2009 at 1:52
  • that was great. can you tell me how do i receive data i just sent into php?
    – amit
    Dec 21, 2009 at 2:11
  • The first example shows receiving HTML from PHP and putting it into a comment box. All jquery Ajax calls take a function callback where you can pass HTML, XML or JSON data back from the server.
    – cletus
    Dec 21, 2009 at 2:13
  • what i mean is in my code i receive a variable 'vote' into user_submit.php. but in the jquery example, how should i receive daya into user_submit.php?
    – amit
    Dec 21, 2009 at 2:15
  • What is daya? Perhaps you should pose a second question explaining exactly what you want to do rather than trying to do this in comments or revising the old question.
    – cletus
    Dec 21, 2009 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.