Hi all,
I'm having some problems with a piece of ajax code. I have a page with a couple of forms, all with the same name. On submit, I want to use Ajax to send the form. For now, I only want to output the content of a simple txt file into a div on my page, just so I know it works.
Though, whatever I try, the readystate of the request stays 0. What am I doing wrong?
window.onload = initAll;
var xhr = false;
function initAll(){
var allForms = document.getElementsByName('edit_module_form');
for(var i=0; i < allForms.length; i++){
allForms[i].onSubmit = submitForm;
}
}
function submitForm()
{
if (window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}
else{
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
if(xhr){
xhr.onreadystatechange = send_form;
xhr.open(GET, "result.txt", true);
xhr.send(null);
}
return false;
}
function send_form(){
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var outMsg = xhr.responseText;
}
else {
var outMsg = "There was a problem with the request " + xhr.status;
}
alert(outMsg);
}
}