Hi,
I'm building a basic forum where every post contains some text, first and last name, and the date the message was written.
I'd like to have the board update with AJAX constantly, and to add new messages on the fly as they are written.
I have a file, getlatest.php?date=...
that retrieves all messages from the date in the $_GET
till NOW()
.
It returns the data as an array of objects. Lastly, I JSON-encode the data.
I call this function from some AJAX code, like so:
setInterval("update()", 5000);
function update(){
$.get("getlatest.php", {
date: "2009-06-23_16:22:12" //this is just a date I
//entered for testing
}, function(forumdata){
//do something with forumdata here?
}, "json");
}
Now I have the data within forumdata
, as in forumdata[0].first_name
ect.
I would like to now use PHP to display the comment, like so:
$forumdata = json_decode(forumdata);
foreach ($forumdata as $value)
{
$newpost = new Post($value); // Post being some class that gets the data from
// $value and converts it to HTML + CSS
$newpost->displayPost(); // some function that echo's the HTML
}
I realize what I'm trying to accomplish here is slightly unfeasible, since PHP is a server-side language, and the calculations are at this point client-side, but is there some way (maybe through AJAX?) that I could use PHP to manage the data, once I retrieve it through javascript?
My main reason for wanting to do this is my total lack of knowledge in javascript, so any alternatives for translating the forumdata
variable into blocks of HTML and CSS are also great for me.