I have Webpage written using PHP that displays data from MySQL tables. I want my page to refresh after new records are entered to the database.

Recommended Answers

All 8 Replies

There is no way to refresh a page as and when the data is updated (Unless you wanted to set up some sort of AJAX check) but the easiest way of doing this is placing this code:

<?php header('Refresh: 5'); ?>

on the first line of your script (This will refresh every 5 seconds but you can change that)

love_bug>I have Webpage written using PHP that displays data from MySQL tables. I want my page to refresh after new records are entered to the database.

I think you want to fetch all data and paint on the page once new record is added. Itsn't it?

Where is your code?

Why not just reload the page using a submit button (most likely the same one you used to add the new records)?

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>"></form>

Or use a smart idea and use the JavaScript time method to check for a new record using AJAX. Really, can no-one create a chatting system?

Basicly you need to look up javascript counting and that will teach you how to run the function and how spaced apart to run the function. As for the function its self you may want to look into jQuery AJAX as this is a more simplified version of the JavaScript AJAX.

In theory this is what needs doing.
1) Run a JS function every 10 seconds.
2) Run an AJAX statement in the function linking to check.php
3) In the check.php run a query to check the number of rows in the table.
4) Use the returned number from PHP in the JS function to check if it is larger than the current number of rows.
5) if it is then you will need to use AJAX once more to retreive a print out of the result from the MySql database.
7) Use JavaScript to replace the current results with the new one.

If there isn't another row added JS will simply just stop running the script and wait for the elapsed time again to check again.

In theory this is what needs doing.
1) Run a JS function every 10 seconds.
2) Run an AJAX statement in the function linking to check.php
3) In the check.php run a query to check the number of rows in the table.
4) Use the returned number from PHP in the JS function to check if it is larger than the current number of rows.
5) if it is then you will need to use AJAX once more to retreive a print out of the result from the MySql database.
7) Use JavaScript to replace the current results with the new one.

This would be the best solution

In theory this is what needs doing.
1) Run a JS function every 10 seconds.
2) Run an AJAX statement in the function linking to check.php
3) In the check.php run a query to check the number of rows in the table.
4) Use the returned number from PHP in the JS function to check if it is larger than the current number of rows.
5) if it is then you will need to use AJAX once more to retreive a print out of the result from the MySql database.
7) Use JavaScript to replace the current results with the new one.

Care to give some example code for?
1) Run a statement every X seconds
2) links to a form.php
5) You would just link to a form or a function that php would check for recent rows added and return them?
7) Why replace then just add?

Care to give some example code for?
1) Run a statement every X seconds
2) links to a form.php
5) You would just link to a form or a function that php would check for recent rows added and return them?
7) Why replace then just add?

Nope, a good magician never reveals his tricks or a good developer never shares his source :P

Um I would but it takes a while and I'm a little busy.

Plus I would only be able to do it with the jQuery framework (I learnt JS with it) :(

i guess what you need is this in your php file:
<?php
echo '<meta http-equiv=Refresh content="0;url=index.php?reload=1">';
?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.