Jump to content

Send Post data using XMLHTTPREQUEST?


siddscool19

Recommended Posts

using only js and assuming you have a properly created XHR object stored in a variable called "xmlHttp":

function post(){
    if(xmlHttp){
        var url = 'submit.php'; // the url you want to submit to
        var yourdata = document.getElemenyById('yourForm').serialize();
        xmlhttp.onreadystatechange = XMLHttpRequestChange; 
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(yourdata);

 

Observations:

- url can be any URL that will handle the POST, no biggie here

- yourdata must be a set of "GET-style" key-value pairs; either you can format them by hand ie:

var yourdata = "name=myname&age=myage&foo=bar";

if you do it by hand dont forget to URLEncode the values.

- the better way to do the above step is to use the serialize() function instead:

it can accept either a form as a parameter (assign an ID to the form and serialize the form based on its ID as in my example or it can also accept an associative array as a parameter and it will produce "POST-SAFE" content :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.