As of right now, I have a PHP page that automatically refreshes every 8 seconds. When it refreshes, it updates information on the page that it gets from an API. How do I refresh the PHP script [clear the current data/html text, echo the new data] without refreshing the page? Thanks in advance!
3
- use ajax.. you cannot do it with just hp, you need client side javascript language. api.jquery.com/jquery.ajaxUnni Babu– Unni Babu2015-12-20 04:42:25 +00:00Commented Dec 20, 2015 at 4:42
- I'm not sure how... Can you give me a quick example as to how this would be done?Justin G– Justin G2015-12-20 04:43:20 +00:00Commented Dec 20, 2015 at 4:43
- You need to learn to use AJAX with javascript or jQuery.Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ– Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ2015-12-20 04:43:23 +00:00Commented Dec 20, 2015 at 4:43
Add a comment |
2 Answers
create a page that contains ajax code, (javascript or jquery). Here its jquery ajax(its simple)
create another php page and put all required php code there.
So we have page 1 with ajax and page 2 with php code
in page 1
$.ajax({ method: "POST", url: "yourphppage.php", //php page link here data: { name: "John"} // send any data to php page if needed }) .done(function( msg ) { //msg contains the response from php page... alert( "Data Saved: " + msg ); //use msg to update the page without refresh }); now in page 2 add the php code you have... this page 2 will return response to page 1. This is how ajax works and the result is a page that updates without full refresh
1 Comment
Justin G
I see. Where would I put the code to run this script every 8 seconds?
You cannot do it with PHP, you need to make an ajax request by javascript.
An example of how to do it: