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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.