Jump to content

AJAX questions (not completely new, but don't know all the features)


kratsg

Recommended Posts

Ok, I'm kinda confused on how to word this question, but let's say I've got a chatroom for instance.

 

So we post a message, uses ajax to submit the info to the php file that adds the information to the database. If the message has some error, how can I get the php file to send back a javascript alert (like

window.alert('Sorry, this message had an error. Please try again later.');

so that it would automatically pop-up in the user window?

 

Similarly, if someone posts a new message to the database, how can I automatically create an AJAX call to a php file to update the chat room messages to include the new message?

 

Right now, my solution for the latter is to auto-call the php file every 3 seconds and simply grab all the messages (up to a certain point) and re-update the chatroom messages, whether there's a new message or not.

define a javascript function to be used as the connection object's onreadystatechange, for instance:

 

function sndItemReq3(action) {
var http4 = createRequestObject();
http4.open("get", action);
http4.onreadystatechange = function() {
	if (http4.readyState == 4) { // This is the response from the server
		var serverResponse = http4.responseText;

                 if (serverReponse == "error") {
		      window.alert('Sorry, this message had an error. Please try again later.');
                 }
	}

}
http4.send(null);
return 1;
}

 

This function defines the connection object and then defines onreadystatechange as a function. Whenever the connection object performs an action, it changes it's ready state, hence our ability to define what it does onreadystatechange. onreadystatechange == 4 is when we get the response from the server script, typically a string.

define a javascript function to be used as the connection object's onreadystatechange, for instance:

 

function sndItemReq3(action) {
var http4 = createRequestObject();
http4.open("get", action);
http4.onreadystatechange = function() {
	if (http4.readyState == 4) { // This is the response from the server
		var serverResponse = http4.responseText;

                 if (serverReponse == "error") {
		      window.alert('Sorry, this message had an error. Please try again later.');
                 }
	}

}
http4.send(null);
return 1;
}

 

This function defines the connection object and then defines onreadystatechange as a function. Whenever the connection object performs an action, it changes it's ready state, hence our ability to define what it does onreadystatechange. onreadystatechange == 4 is when we get the response from the server script, typically a string.

 

Ok, I understand my first problem and that makes sense. Now, what if I wanted to change the user's php session values? Would I be able to do that through AJAX or would I have to send the user to a new page?

 

And what about my second question? Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.