AJAX calling a PHP code and getting a response every few minutes -


I am trying to create a very simple message board (author, text, and written) which is automatic : Will update in a few moments to see if a new message has arrived, and if it is, then automatically load the latest message (message).

I'm proficient in PHP, but my knowledge in AJAX is low.

By the way I see it, I have to create a PHP file called get_messages.php which will be connected to a database and a $ _ GET All posts beyond the variable return X , and then by any means I will call it in every few minutes with the call of $ _ GET = current time ? Is this sound right?

How do I get information about asynchronously requesting and returning?

You are very close, you will need PHP scripts that can query the database for your results. Are there. After this, you want to change those results into an array, and they:

  $ result = getMyResults (); Assume that it produces the following array: array ("id" =>, "128", "authorid" => "12", "posttime" =>, "12:53 pm", "Comment" = & gt; "I totally agree! StackHowflow FTW!"); * / Print json_encode ($ result); / * We will end with the following JSON: {{"id": "128"}, {"authorid": "12"}, {"posttime": "12:53 pm"}, {"comment": " I fully agree! StackViewFlow FTW! "}} * /  

These results are included in JSON format once, so you better manage them with Javascript You can. Using JQuery, we can do the following:

  setInterval ("update ()", 10000); / * Call server every 10 seconds * / function update () {$ .get ("serverScript.php", {}, function (response) {/ * 'response' is our JSON * / alert.comment;} , "Jason"); }  

Now that you've got your data within JavaScript ('response'), you are free to use the information from the server.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -