?Briella 2014-08-03 15:27 采纳率: 0%
浏览 52

将数据推送到客户端

I like using PHP and JavaScript with jQuery.

I am displaying a list of items out of a SQL database using PHP. I have a button that opens up a new page in a new tab that will add an additional item to the database.

How would I add the newly added item in the database to the currently displayed list on the first page without refreshing that page?

I can force the page to make an ajax request every few seconds but I don't think that is very good practice.

I have been looking into sockets - however I don't know how to apply that knowledge to my current situation.

  • 写回答

3条回答 默认 最新

  • elliott.david 2014-08-03 15:35
    关注

    If you want the server to push the information back to the client, Comet may be what are you looking for.

    Recent browsers support WebSockets, which enables the server to communicate with clients without the need for the client to make regular AJAX requests. Older browsers don't support WebSockets, so if you need to support them, you may be interested in frameworks which allow to fallback to more basic techniques, such as long polling on those browsers.

    Note that there are libraries which make it really easy (well, some don't) to integrate WebSockets or Comet in your web application, so don't reinvent the wheel and stick with the one which is available in the PHP framework you use right now. For example, if it's Laravel, a simple Google search leads me to BrainSocket, "a Laravel package that allows you to get up and running with real-time event-driven PHP apps using WebSockets."

    In all cases, you're not expected to use sockets (unless by sockets, you mean WebSockets in your question).

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题