Using Javascript/ AJAX to post HTML form data

javascript form ajax php

Why would you need to do this? Well for instance if you were using 3rd party AJAX APIs and you wanted to capture the data from them perhaps, or maybe you just have a complex form.

Or you could use this for form validation to return errors from the server without refreshing the browser, for example, if the data already exists in a database you could let the user know.

Either way its pretty simple. Basically we just use javascript to post the form object over http to a serverside page – e.g. PHP, JSP, ASP etc…

First of lets have a look at the javascript


And thats it, as for the HTML form, only thing to note is that the form action calls the sendData function:

* Address Line 1:
Address Line 2:
Address Line 3:
* Town:
* Postcode:
 
Server-Response:

Oh and of course the post.php page, in here you could add in your validation, check the database etc... what ever you ourput to the page (using print, echo etc...) can be then sent back to your form page and displayed without the user leaving your form page or refreshing the browser.


Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>