i started doing some AJAX yesterday, and I've hit a bit of a hurdle that i can't find anything definitive on, so i thought i'd ask the good people of OcUK.
i have my AJAX functions:
but i can't seem to figure out logically where my loading message would go. I'm assuming that i've got to tell the script that whilst the readyState is less than 4, to display the loading message...but i can't figure out where/how. i'm really new to Javascript as well...although it's come on leaps and bounds since yesterday morning! :/ if anyone can help, i'd be very grateful.
and in case anyone was wondering, there's checking in the php script as well, i've not left it that open
i have my AJAX functions:
Code:
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3) {
req = false;
}
}
}
return req;
}
var http = getXMLHTTPRequest();
function checkCommentForm()
{
var name = document.commentForm.name.value;
var email = document.commentForm.email.value;
var url = document.commentForm.url.value;
var comment = document.commentForm.comment.value;
if (name == "")
{
document.getElementById('errorName').innerHTML = 'this field hungry...feed him your name!';
}
else if (email == "")
{
document.getElementById('errorEmail').innerHTML = 'this field hungry...he eat email address!';
document.getElementById('errorName').style.display = 'none';
}
else if (comment == "")
{
document.getElementById('errorComment').innerHTML = 'this field very hungry...feed him a comment!';
document.getElementById('errorName').style.display = 'none';
document.getElementById('errorEmail').style.display = 'none';
}
else
{
postComment();
}
}
function postComment()
{
var pid = document.commentForm.pid.value;
var name = document.commentForm.name.value;
var email = document.commentForm.email.value;
var url = document.commentForm.url.value;
var comment = document.commentForm.comment.value;
var url = "makeComment.php?name="+name+"&email="+email+"&url="+url+"&comment="+comment+"&pid="+pid;
myRand = parseInt(Math.random()*999999999999999);
var modurl = url+"&rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = responseComment;
http.send(null);
}
function responseComment()
{
if (http.readyState == 4)
{
if (http.status == 200)
{
var name = document.commentForm.name.value;
document.getElementById('posted').style.display = 'block';
document.getElementById('posted').innerHTML = "<h2>"+http.responseText+"</h2>";
document.getElementById('commentForm').style.display = 'none';
}
else
{
document.getElementById('posted').innerHTML = "<h2>"+http.statusText+"</h2>";
}
}
}
but i can't seem to figure out logically where my loading message would go. I'm assuming that i've got to tell the script that whilst the readyState is less than 4, to display the loading message...but i can't figure out where/how. i'm really new to Javascript as well...although it's come on leaps and bounds since yesterday morning! :/ if anyone can help, i'd be very grateful.
and in case anyone was wondering, there's checking in the php script as well, i've not left it that open
