Jump to content
xisto Community
Sign in to follow this  
ewcreators

Real Time Updating With Ajax I need a bit help with ajax in this real time chat script.

Recommended Posts

<script type='text/javascript'>function submitForm(){ 	var xhr; 	try { xhr = new XMLHttpRequest(); }				 	catch(e) 	{		  xhr = new ActiveXObject(Microsoft.XMLHTTP);	} xhr.onreadystatechange =function(){if(xhr.readyState == 4) {if(xhr.status == 200) document.getElementById('c').innerHTML="SENT";elsedocument.ajax.dyn.value="ERROR CODE" + xhr.status;}};var data=document.ajax.dyn.value;//var chat=document.ajax.ba.value;var final="&content="+data;xhr.open("GET","chat2.php?ba=<?php echo $_GET[a];?>" +final,true);xhr.send(data);}</script><?phprequire('connection2.php');require('sessions.php');require('details.php');$sel=mysql_query("SELECT * FROM `chat` ORDER BY `id` LIMIT 1,6 ");echo "<table border='1'> <tr><th colspan=2>SIDECHAT</th></tr><tr><td colspan=2>";while($row=mysql_fetch_array($sel)) {echo "<font color='yellow'><b>".$row['name']."</b></font>:<font color='gray'>".$row['content']."</font><br />";}echo "</td></tr><br /><tr><td colspan=2> <div id='c'> </div></tr></td></tr>";echo "<tr><td colspan=1><FORM method='POST' name='ajax' action=''><INPUT type='text' name='dyn' maxlength='40'></td><td><input type='submit' onClick='submitForm()' value='>' ></td></tr>";require('links.php');?>

Now when that result shows, (it comes real time afcourse), how can i get others to show their results real time without have the current user to refresh/go to a different page?
like msn ..etc you get the results directly real time/at the same time.
How can i go about that?
Also, i want to keep upto 6 results in my real time chat box, but the only method i know is off 1,6...which will only select the first 6 rows..please help!

Share this post


Link to post
Share on other sites

Now when that result shows, (it comes real time afcourse), how can i get others to show their results real time without have the current user to refresh/go to a different page?

Are you asking how to get other results? Why don't you just make a function that sends tat via ajax, and then call it again with a settimeout function?

Also, i want to keep upto 6 results in my real time chat box, but the only method i know is off 1,6...which will only select the first 6 rows..please help!

Once again, I am sorry but I just don't always know what your asking exactly. I believe your delema is that its only selecting the first 6 rows insted of the most recent 6 rows am i right? This could be solved by using the ORDER BY in sql. I see you have ORDER BY `id` already but it is in the wrong order. You want to add DESC after that
ORDER BY `id` DESC
to reverse the order that it gets the results by.

Share this post


Link to post
Share on other sites

I did a search on google and found this.

http://forums.xisto.com/no_longer_exists/

SELECT RecordIdFROM TableWHERE RecordIdIN (    SELECT RecordId     FROM Table     ORDER BY RecordId DESC     LIMIT 100)ORDER BY RecordId ASC

as for setTimeOut()

I've used it twice.I think this would work
}
linenums:0'>function Ajax*() { ... ..setTimeOut('Ajax*();', 2000 );}
I think what you want to use for this function is for <body onload='' so that it start right away refreshing the post. Im not sure how it will work when you submit the from with another ajax update. I think it will continue working. Good luck.
Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

btw, your ORDER BY `id` DESC idea didnt work :Pit just grabbed the rows 1,6 which were desc in id order.
meaning id 1,2,3,4,5,6


Your limit 1,6 is messing it up because it's counteracting with the order. Just type LIMIT 6... that will limit it to the last 6 rows.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.