0 like 0 dislike
265 views
how to load the data from the database with refreshing the page in php
asked Nov 7, 2014 in Education & Reference by Simmi (530 points) | 265 views
    

1 Answer

0 like 0 dislike

So you have a div that you would like to be up to date on your site without having the page refresh? This small script will allow you do this. Let's say that you want to show the total number of users on your site as your site is blowing up and you want visitors to see the number grow. firstly...load jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

next, we need to build the script in the "head" section of your page. This script targets a div with a loaded php file and sets how often the div should be reloaded

<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#divToRefresh').load('/path/to/your/php/file/userCount.php');
}, 3000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
});
// ]]></script>

here are the contents of "userCount.php"

require "/path/to/your/database/connection/connection.php";
$userCount = mysql_query( "SELECT * FROM users" ) or die("SELECT Error: ".mysql_error());
$numRows = mysql_num_rows($userCount);
echo $numRows;

here is the div in the html. you can place "loading users..." or a "loading" image inside of the div and it will be replaced by the results of "userCount.php"

<div id="divToRefresh">Loading users...</div>
answered Nov 7, 2014 by sandhu

Related questions

1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Sep 13, 2013 in Education & Reference by Asraf | 525 views
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
3 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

Mathe Forum Schule und Studenten
1,161 questions
1,297 answers
271 comments
1,558 users