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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.