PHP自动刷新页面而不丢失用户输入

发布于 2024-09-04 09:58:03 字数 250 浏览 0 评论 0原文

我正在开发一个 PHP 协作软件项目。我有一个页面,显示向数据库添加内容的其他用户的最新更新,但也有一个表单输入以允许用户输入文本。我目前正在使用此代码每 30 秒自动刷新页面:

header('Refresh: 30');

问题是标题代码刷新整个页面,而不仅仅是从数据库中提取的内容。是否有任何 PHP 代码可以只从数据库中提取任何新数据而不刷新整个页面?

如果有人能指出我正确的方向,我将不胜感激。

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 someone could point me in the right direction I'd appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-09-11 09:58:03

PHP 仅在浏览器向服务器发出新请求时运行。如果您要求浏览器刷新页面,它就会这样做。

如果您想在页面加载后动态更改页面内容,则必须使用 JavaScript 和 AJAX。 jQuery 库在 Stack Overflow 上广泛讨论并在网络上广泛使用,使得 AJAX 请求特别容易执行。安装库后,您可以编写如下内容:

$("#element-to-refresh").load("your-page.php #element-to-refresh");

这是一种粗略的方法,会生成并传输大量不必要的内容,但它确实有效。对于更优雅的方法,请考虑一个仅生成相关数据的脚本,您只能在刷新时调用这些数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文