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:


# ... connection string to database ...
$q = mysql_query('select * from message_board order by id desc');

<!DOCTYPE html>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script type="text/javascript">

        $('#myform').submit(function(e) {
            var mdata = $('#myform').serialize();

            // if searchString is not empty
            if($('#message').length > 0) {
                // ajax call
                    type: "POST",
                    url: "save.php",
                    data: $('#myform').serialize(),
                    timeout: 30000, // 30 seconds
                    cache: false,
                    beforeSend: function(html) { // this happens before actual call
                    error: function(){
                        $("#result").hide().html('server not responding');
                    success: function(msg){ // this happens after we get results

                return false;

<form id="myform" method="post" action="save.php">
    <textarea class="text" name="message" id="message"></textarea>
    <input type="submit" name="submit" id="submit" value="submit" />

<!-- this is where Ajax will display the result -->
<div id="result"></div>

<!-- this is where old data will be displayed -->
<div id="messages">

        if(mysql_num_rows($q) > 0)
            while($r = mysql_fetch_assoc($q))
                echo '<div class="block">
                    <p class="date">'.$r['creation_date'].'</p>
                    <p class="message">'.$r['message'].'</p>


And this is the script which receives data from the form, i.e. save.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>

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.


Member Avatar

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.

commented: thank you for suggestion, I have to try it! +9

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.