View Full Version : How to run this AJAX request when form is submited

11-26-2008, 12:20 AM
I have this ajax request code and I know how to make it run onClick, but I would like to run it when my form is submited, regardless of wheather user clicked submit or pressed enter.

Also, I read an article that says GET is the faster method, true?

Here is my Ajax request:

var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();

function replace(where, wheretwo, what) {

http.open("GET", "go.php?id=" + what, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
var data = http.responseText.split("[BRK]");
document.getElementById(where).innerHTML = data[0];
document.getElementById(wheretwo).innerHTML = data[1];

11-26-2008, 02:36 AM
Than call it onsubmit of the form.


Arty Effem
11-26-2008, 03:35 AM
Than call it onsubmit of the form.

EricWon't the document be dismissed before the request gets a chance to terminate?
I would suggest calling it in the onsubmit handler but have the onsubmit handler return false to prevent submission. Then call the form's submit method as the last statement in the readyState==4 block.

11-26-2008, 05:18 AM
Yes the poster would have to cancel the form submission. I am sure they could have figured that out. :)