kratsg Posted October 13, 2007 Share Posted October 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 13, 2007 Share Posted October 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 13, 2007 Author Share Posted October 13, 2007 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? Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 13, 2007 Author Share Posted October 13, 2007 BUMP (sorry, nobody's answered second question yet) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.