Posting a Form with Ajax.

Expanding on our current xmlhttp request function. We wanted the flexibility to send not only via the url scope but also the form.

So to expand on our function we had to do the following.

set up our parameters we want to send

var parameters = "txtarea1=" + encodeURI( document.getElementById("txtarea1").value ) + "&txtarea2=" + encodeURI(document.getElementById("txtarea2").value );

Set up our headers

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);

And then send the paramaters

xmlhttp.send(parameters);

To test your results set up a cfdump(form) or an output to file on the action page and you should be able to see the form parameters you sent.

Check this page out as another example, this example also has some neat error handling in it.

Related Blog Entries

Comments