Home » , , » Auto Load and Refresh Div every 10 Seconds with jQuery

Auto Load and Refresh Div every 10 Seconds with jQuery

Unknown | 9:13 PM | 0 comments
Have you seen this Twitter Search and Facebook shows most recent tweets/posts count from the database every 10 seconds
on top of the page. with jQuery and Ajax It's simple just 5 lines of code.

Example 1

Index.html
Contains javascript and HTML code. Take a look at load("record_cound.php").




<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_tweets').load('record_count.php').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds

<body>
<div id="load_tweets"> </div>
</body>

</script>


record_count.php Just printing "CodeSlot.Info" every 10 seconds

<?
echo 'Just printing CodeSlot.Info every 10 seconds';
?>


Example 2 index.php Contains PHP code you have to pass the search box value twitter.com/search?q="some thing" to $search_word in facebook pass the user id or user session value.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_tweets').load('record_count.php?q=<?php echo $search_word; ?>').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds

<body>
<div id="load_tweets"> </div>
</body>

</script>


record_count.php Counting tweets/posts from the database.

<?php
$search_word=$_GET['q'];
$sql = mysql_query("Select id form Messages where message
LIKE '%$search_word%'");
$record_count=mysql_num_rows($sql);
//Display count.........
echo $record_count;
?>


Share this article :

0 comments:

Post a Comment

Notice : Spam Comments Will Be Deleted

 
Copyright © 2012. CodeSlot - All Rights Reserved
Template By Phoenix.Elmasry
Powered By Blogger