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. Link to comment https://forums.phpfreaks.com/topic/73126-ajax-questions-not-completely-new-but-dont-know-all-the-features/ 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. Link to comment https://forums.phpfreaks.com/topic/73126-ajax-questions-not-completely-new-but-dont-know-all-the-features/#findComment-368805 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? Link to comment https://forums.phpfreaks.com/topic/73126-ajax-questions-not-completely-new-but-dont-know-all-the-features/#findComment-368821 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) Link to comment https://forums.phpfreaks.com/topic/73126-ajax-questions-not-completely-new-but-dont-know-all-the-features/#findComment-368857 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.