I am trying to make a dynamic comment insert system using AJAX, but I have a problem with the form: it won't even try and perform the javascript function. Instead, it just performs a GET operation and redirects me to ?user=&reply=&submit=Submit
and doesn't do anything. Here is my form code:
<form id="cmtsubmit" onsubmit="return submitComment(this);">
<input type="text" name="user" id="user" />
<textarea name="reply" id="reply"></textarea>
<input type="submit" name="submit" value="Submit" />
</form>
and here is my javascript code:
function submitComment(form) {
if (window.XMLhttpRequest) {
httpRequest = new XMLhttpRequest();
} else if (window.ActiveXObject) {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.onreadystatechange = function() {
if(httpRequest.readyState === 4 && httpRequest.status === 200) {
document.getElementById('container').innerHTML += '<div style="border: 1px solid; padding: 15px;">' + httpRequest.responseText + '</div>';
return false;
} else {
}
}
httpRequest.open('GET', 'index.php?p=cmtinsrt&user=' + form.user.value + '&cmt=' + form.reply.value, true);
httpRequest.send(null);
}
Where am I going wrong?
EDIT: I added a "return false" to the code and now it works in IE10, but not Firefox or Chrome. I haven't tested any other browsers yet but clearly if it doesn't work in those two it's not a proper solution.