I'm working on a PHP collaboration software project. I have a page that shows the latest updates from other users who are adding content to the database, but also has a form input to allow the user to enter text. I am currently using this code to refresh the page automatically every 30 seconds:
header('Refresh: 30');
The problem is that the header code refreshes the entire page, and not just what is pulled from the database. Is there any PHP code that will just pull any new data from the database without refreshing the entire page?
If som开发者_C百科eone could point me in the right direction I'd appreciate it.
PHP runs only when the browser makes a new request to the server. If you're asking the browser to refresh the page, it will do precisely that.
If you want to dynamically change the content of the page after it's loaded, you'll have to use JavaScript and AJAX. The jQuery library, discussed widely on Stack Overflow and used widely around the web, makes AJAX requests particularly easy to execute. With the library installed, you can write something like:
$("#element-to-refresh").load("your-page.php #element-to-refresh");
This is a crude approach that generates and transfers a lot of unnecessary content, but it works. For a more elegant approach, consider a script that generates only the relevant data which you can call only when you're refreshing.
精彩评论