Hello everyone, I have developed an application. But I need a script to perform push back just like facebook does when updating users profile. Please how can I do this in php. Please I would like it with a simple demo code. Thanks alot.
What do you mean with "push back"?
You know when you are on facebook. if a user post an information on your wall. web page updates the information in real time. so this is what I mean. How did facebook achieve that. allowing a page to update and display content to viewers seemlessly without pooling from the server.
Ok, you need Ajax and a script to receive, save and return data from the form, FB uses also a cache to save data and return, here I create an example with JQuery framework, we are going to save it only to the database:
create table message_board(id int(9) not null auto_increment primary key, message varchar(255) not null, creation_date datetime not null) engine=myisam default charset=utf8;
The form page:
And this is the script which receives data from the form, i.e. save.php:
<?php # ... connection string to database ... $m = $_POST['message']; $d = date('Y-m-d G:i:s'); $q = mysql_query("insert into message_board(message,creation_date) values('$m','$d')") or die(mysql_error()); echo '<div class="block"> <p class="date">'.$d.'</p> <p class="message">'.$m.'</p> </div>'; ?>
Now, since this is an example if you double post without reloading the page
#result will be overwritten, but by reloading the page you get also the previous message. If you want to create something like that consider using mysqli: http://php.net/manual/en/book.mysqli.php
note: you can also choose to get all data from Ajax by looping the script in save.php instead of returning the last update.
Edited by cereal
If you need 'push' tech, then ajax ('pull') is pretty poor, performance-wise, as you have to continuously poll the server. Comet (sometimes called 'long-polling') may be a better fit for you, but it may mean setting up a server. The latest, 'websockets' looks extremely promising if only that it worked across all browsers.
For Comet - see the AjaxPushEngine - http://www.ape-project.org/ajax-push.html
Here's an article on SO:
may have links to implement websockets x-browser
Comet doesn't play nicely with PHP.
Edited by diafol
yeah tanks. I have personally researched on it myself. currently on the project I have been using ajax pool. But I realized it is very poor. cause it hangs the server. I also tried to use ape-project but when I downloaded I noticed it is written in c and I could not find a tutorial. All the demo source code were difficult to understand with out a tutorial.
I would have used socket but the project is very large and I mite not be able to control sockets on every part since i am new to it.
and socket as limited surport on some browser.