Jump to content

Can I Interrogate Ajax Return Data?


chibineku
 Share

Recommended Posts

As many of you may know, I am making a little chat app. I have thus far been fetching the most recent 10 items from the chat buffer table in my database and echoing those as the data for my ajax callback function. That's fine, but it gives a very short conversation history and I would like to change it. I have adapted the query to only fetch the latest one record and append it to the chat window. Obviously that will recursively spit out the last record - not what I want. So I altered the query to fetch the last record if it's newer than 2 seconds old, because that's how often the javascript checks for new records. That resulted in duplicate entries because its hard to hit return in exactly the right rhythm. So, I changed it to one second and it worked for me testing it on my own, but someone else came in and we couldn't see each other's messages. So, I figured I would include a hidden field holding a timestamp of each message and check the date of the ajax return data against the date of the last message in the chat window. But is it possible to interrogate the ajax return data like this? It would look something like this:<div class="poster">username</div><div class="message" value="2009:10:20 10:32:45">message text</div>This is also, of course, the format the last message in the chat window would take. So, is it possible to do, say:if ( $('#chatWindow :last-child .message').val() !> $(data') .filter('.message').val() ) {$('#chatWindow').append(data);}??

Link to comment
Share on other sites

I don't think that would work, a div element doesn't have a value property. You may be able to use getAttribute to get the value attribute, but I don't think adding your own value attribute will automatically make a value property if the element doesn't already have one.An alternative is to have the PHP return the messages in another format, like JSON. Check if jQuery has support for handling JSON responses from the server, I'm sure that's in there. It would save bandwidth too, you could return a smaller response and then process the response in Javascript to check and print the messages.

Link to comment
Share on other sites

It does have native JSON support, I was merely reluctant to use it because I never have before - but there's nothing wrong with a little learning. JSON returns the data as an object, is that correct, so I could use jQuery methods on it to find out its datestamp? I figured divs didn't have any use for value attributes but figured I could still give them one. I originally thought of storing the timestamp in a span with display:none - anywhere it won't be seen or affect the page, really. I'll read up on JSON.

Link to comment
Share on other sites

PHP has good support for JSON, it's got the json_encode function you can use to convert a PHP array to a JSON structure. e.g.:

$message = array(  'poster' => 'Joe',  'message' => 'message here',  'date' => 'xxx');echo json_encode($message);

In Javascript you would convert that to an object using jQuery, and then you could access something like response.date to get the date property.

Link to comment
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
 Share

×
×
  • Create New...