Friday, June 13, 2014

Auto Refresh using PHP & Jquery

In this post i want to explain how to autorefresh data's.We can fetch values without loading whole page using this script.Most popular Social websites are using this script like twitter,facebook etc.

Let's have look.


Live DemoDownload Script

Create table using the following command.

Mysql Table

        CREATE TABLE IF NOT EXISTS `users` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `uname` varchar(225) NOT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

conn.php

<?php
        $conn=mysql_connect("localhost","root","");
        $db=mysql_select_db("autorefresh",$conn); //replace with your DB name
    ?>

JQuery Function

<script type="text/javascript">
    var autorefresh = setInterval(
    function (){
        $('#items').load('count.php').fadeIn("slow"); //Include PHp file for dynamic update
    }, 1000); //refresh every 1000 milliseconds
  
    </script>

Display dynamic results using PHP.I have stored values in mysak and retrived from table.

PHP function

<?php
    $res=mysql_query("select * from users order by id ASC"); ?>
<table width="500" border="1" cellspacing="0" cellpadding="0" style="border:1px solid #ccc;">
  <tr style="border-bottom:1px solid #ccc">
    <td width="250" style="padding:5px;">SNo</td>
    <td width="250" style="padding:5px;">Username</td>
  </tr>
  <?php
    while($row=mysql_fetch_array($res)){ ?>
  <tr>
    <td width="250" style="padding:5px;"><?php echo $row['id'];?></td>
    <td width="250" style="padding:5px;"><?php echo $row['uname'];?></td>
  </tr>
  <?php } ?>
</table>


Please share your comments and feedback.Thanks.Please subscribe my updates via email.

No comments:

Post a Comment