如何自动更新内容而无需使用php / ajax重新加载网页? [英] how to update content automatically without reloading webpage using php/ajax?

查看:151
本文介绍了如何自动更新内容而无需使用php / ajax重新加载网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP创建拍卖工具。我遇到的问题(我欣赏它的一个基本的,但我需要澄清)是,我不明白如何自动更新每个用户屏幕上的拍卖价格,而不必采取任何行动或不会导致完全重新加载页面。

到目前为止,我明白Ajax是用来做这件事的,但如果有人能指出我正确的方向或任何有用的材料。我迄今为止的项目计划是使用PHP和JavaScript,因此任何解决方案都需要与这些语言兼容。

注意:我正在使用MySQL数据库。

解决方案

你问的问题有很多可能的答案,他们可以填写整本书。

最简单的方法是使用setInterval()函数和AJAX调用的组合每隔几秒进行一次ajax调用。您几乎每隔几秒就会发出一次AJAX请求:

  setInterval(function(){
$ .get(anyChanges .php,函数(data){
//对返回的数据做一些事情,也许更新一个表或者
});
},3000);

在服务器端 anyChanges.php 会返回一些数据立即得到确认,例如确认某件事情已经发生变化以及新数据。

长时间轮询是谷歌和其他人如何做的。这和上面的例子一样。区别在于服务器端。 anyChanges.php 不会立即返回,脚本会保持连接处于打开状态,直到有新的更改并返回它们。如果您使用长轮询,则通常将间隔设置得更长,例如30秒。

在我看来,最好的方法是使用WEB套接字。这是一项非常新的技术。使用网络套接字,您可以创建与服务器的双向连接。这意味着服务器可以简单地将数据发送到客户端,而不必每隔几秒要求新数据。在PHP中,使用web套接字有点困难(或者我听说过),但是你可以试试看。如果您选择网络套接字,请先尝试了解它们:
tutsplus教程



这个库将是有用的:
socketo.me


I'm trying to create an auction tool using PHP. The problem I'm having (and I appreciate its a basic one but I need clarification) is that I don't understand how to update the "auction price" automatically on each users screen without them having to take any action or without causing a full reload of the page.

So far I understand that Ajax is used to do this but if anyone could point me in the right direction or to any useful materials. My plan for my project so far is to use PHP and JavaScript so any solution would need to be compatible with these languages.

Note: I'm using a MySQL database.

解决方案

Ther question you asked has so much possible answers, they could fill a whole book.

The simplest way to do this is to make an ajax call every few seconds using a combination of the setInterval() function and AJAX calls. You basically make an AJAX request every few seconds:

setInterval(function(){
$.get( "anyChanges.php", function( data ) {
  //do something with the returned data. Maybe update a table or something
});
}, 3000);

On server side anyChanges.php returns some data immediately, like confirmation that something has changed and the new data.

Long polling is how Google and others do it. It's the same as the example above. The difference is on the server side. anyChanges.php would not return immediately, the script would keep the connection open until there is some new changes and return them. If you use long polling, you usually set the interval to longer, for example 30 seconds.

The best way to do it in my opinion, are WEB Sockets. It is a very new technology. With web sockets you can create a two-way connection to the server. That means that the server could simply send data to the clients without them having to ask for new data every few seconds. In PHP it's a little difficult to use web sockets (Or so I heard), but you could give it a shot. If you choose web sockets, try to learn about them first: tutsplus tutorial

This library will be helpfull: socketo.me

这篇关于如何自动更新内容而无需使用php / ajax重新加载网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆