php 在php中刷新部分网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1153535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
refresh a part of webpage in php
提问by Sampson
i develop a page in that i need to refresh a part of a webpage,not a whole one in php using Ajax. Please help me to do this , thanks in advance
我开发了一个页面,因为我需要使用 Ajax 在 php 中刷新网页的一部分,而不是整个网页。请帮助我做到这一点,提前致谢
回答by Sampson
PHP cannot do this, only a client-side language like JavaScript can. That being said, the jQuery librarywould allow you to do this really easily with its AJAX functionality.
PHP 不能做到这一点,只有像 JavaScript 这样的客户端语言可以。话虽如此,jQuery 库将允许您通过其AJAX 功能轻松完成此操作。
Index.php
索引.php
<div id="scores"><!-- score data here --></div>
Could be refreshed with the following JavaScript:
可以使用以下 JavaScript 刷新:
$("#scores").load("index.php #scores");
That would load the contents of #scorefrom index again without refreshing the entire page.
这将#score再次加载from index的内容而无需刷新整个页面。
You could even automate it to refresh every 30 seconds using setInterval();
您甚至可以使用setInterval();每 30 秒自动刷新一次;
var $scores = $("#scores");
setInterval(function () {
$scores.load("index.php #scores");
}, 30000);
You can read more about $.load()at http://api.jquery.com/load/#loading-page-fragments.
您可以$.load()在http://api.jquery.com/load/#loading-page-fragments阅读更多信息。
回答by Mark Biek
Here's a basic example using PrototypeJS.
这是一个使用PrototypeJS的基本示例。
new Ajax.Updater('containerId', '/url/to/get/content', {
parameters: { somename: 'somevalue' }
});
- The first argument is the ID of the container to put the result of the Ajax call in.
- The second argument is the URL to send the request to
- The third argument, in its most basic form, is the list of parameters to send to the URL.
- 第一个参数是放置 Ajax 调用结果的容器的 ID。
- 第二个参数是将请求发送到的 URL
- 第三个参数的最基本形式是要发送到 URL 的参数列表。
For more details about the Prototype Ajax request, check out the Ajax.Requestdocumentation.
有关 Prototype Ajax 请求的更多详细信息,请查看Ajax.Request文档。
Taking a page from Jonathan'snice jQuery answer, here's how you'd execute the Ajax request on a timer using Prototype's PeriodicalExecuter.
从Jonathan很好的 jQuery 答案中获取一个页面,以下是您如何使用 Prototype 的PeriodicalExecuter在计时器上执行 Ajax 请求。
new PeriodicalExecuter(function(pe) {
new Ajax.Updater('containerId', '/url/to/get/content', {
parameters: { somename: 'somevalue' }
});
}, 30);
回答by Quentin
There is a good guide to how the XMLHttpRequest object works over at http://www.jibbering.com/2002/4/httprequest.html
在http://www.jibbering.com/2002/4/httprequest.html 上有一个关于 XMLHttpRequest 对象如何工作的很好的指南
You just need to use that, with whatever condition you want to trigger your refresh on, and a PHP script that will output just the data you care about.
您只需要使用它,在您想要触发刷新的任何条件下,以及一个仅输出您关心的数据的 PHP 脚本。
回答by Bassel Safadi
fastest way is to use jqueryload function
最快的方法是使用jquery加载功能
let's say the contents you want to change are inside a div
假设您要更改的内容在 div 内
then you can simply:
那么你可以简单地:
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$().ready(function() {
$("#dynamic").load("http://url/to/the/dynamic/data");
});
</script>

