如何从另一个页面更改页面内容 [英] How to change page content from another page

查看:59
本文介绍了如何从另一个页面更改页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此功能添加到我的PHP购物车中

假设我们有两个页面:catalog.php和cart.php

我想做的是:

用户单击catalog.php上的添加到购物车"按钮并通过jQuery触发对cart.php的ajax请求,向其发送有关添加了哪种产品的信息(这一切均按预期方式进行),并通过包括刚添加的产品而不进行页面刷新来使cart.php页面进行自身更新(这部分内容)我无法上班).

如果我将两个页面并排放置,然后单击添加到购物车",则什么也没有发生,仅在页面刷新(cart.php)上,我看到添加了新产品.

有没有办法存档?

我还不够清楚,对不起我的不好页面以标准方式显示,没有框架没有弹出窗口该应用程序"按预期工作,这对用户而言不太可能成为问题并排"只是因为我想知道一种获得此功能的方法,因为我将来会看到自己将其用于几乎所有东西(来自pageA,CSS等的对pageB的DOM操作).

解决方案

您可以 通过在 Cart.php 上进行AJAX调用来模拟此过程,该代码会检查(session/db),看看是否已添加新内容.

我建议类似...

  • Cart.php每10至60秒进行一次AJAX调用,并要求在购物车中提供完整的产品列表.
  • 将适当地将任何新项目添加到表中(通过检查产品ID或OrderItem ID).您还应该更新数量/等.

通过这种方式,无论使用哪种机制添加商品,购物车都会在不久之后看到它们.

很显然,您轮询的频率越高,购物车的更新速度就越快,但您对系统的负担也就越大.

现在之所以不可能直接成为原因,是因为AJAX请求始终由客户端而不是服务器发起.

还有另一种可能的解决方法,它可以忽略频繁的轮询并提供近乎即时的更新,但是实现起来却有些复杂.

这被称为长轮询(请参阅此答案),实际上这是怎么回事...

  • 客户端向服务器发送AJAX("A")请求以获取购物车信息.
  • 服务器接受请求,但不执行任何操作(就像正在处理长时间运行的脚本一样)
  • 当通过AJAX("B")接收到新的购物车商品时,服务器将使用详细信息响应请求A,并且购物车"页面会相应地更新表格.
  • 如果在合理的超时时间(30-120秒)内未检测到购物车活动,则服务器将以无操作"响应进行响应并关闭连接.
  • 无论客户端收到什么响应,它都会立即打开一个新的AJAX请求并重新开始.

有效地,您的PHP脚本随后处理检查数据库/会话/等是否有更新,并且客户端仅在慢速"服务器上等待.这就是twitter通过API实现其各种供稿的方式-每个新的tweet在生成时都会在新行中返回.

请注意,这可能很难实现,因为您只是将轮询从客户端转移到了PHP,但是从JavaScript的角度来看,它确实使其更加优雅,消除了延迟并减少了浪费的网络开销./p>

I'm trying to add this functionality to my PHP cart

Let's suppose we have two pages: catalog.php and cart.php

What I would like to do is:

User clicks on "Add to cart" button on catalog.php and triggers an ajax request via jQuery to cart.php sending it info about which product was added (this all works as expected) and makes the cart.php page update itself by including the just added product without a page refresh (this is the part I can't get to work).

If I put the two pages side by side and click "Add to cart" nothing happens, only on page refresh (cart.php) I see that the new product was added.

Is there a way to archieve this?

EDIT: I wasn't clear enough i'm sorry my bad The pages are presented in a standard way, no frames no popups The "app" works as expected and this will unlikely be an issue for users The "side-by-side" thing was just because i would like to know a way to obtain this functionality since i can see myself using it in the future for pretty much anything (DOM manipulation of pageB from pageA, CSS, etc.)

解决方案

You can simulate this by having an AJAX call on Cart.php which checks the (session/db) to see if something new has been added.

I'd suggest something like...

  • Cart.php makes an AJAX call every 10-60 seconds and asks for a complete list of products in the cart.
  • Any new items are added to the table as appropriate (either by checking product Ids or OrderItem Ids). You should also update quantities/etc.

This way, no matter what mechanism is used to add items, the cart will see them before too long.

Obviously, the more often you poll, the quicker the cart will update but the more load you'll put on the system.

Now the reason it's not directly possible is that AJAX requests are always initiated by the client, not the server.

There is one other potential workaround which omits frequent polling and gives near-instant updates but it's a little more complex to implement.

It's known as long polling (see this answer) and effectively what happens is this...

  • Client sends AJAX ("A") request to the server for cart information.
  • The server accepts the request but does nothing (as if a long-running script were processing)
  • When a new shopping cart item is received via AJAX ("B"), the server responds to Request A with details and The cart page updates the table as appropriate.
  • If no cart activity is detected within a reasonable timeout (30-120s), the server responds with a "No Operation" response and closes the connection.
  • Whichever response the client receives, it immediately opens a new AJAX request and starts all over again.

Effectively, your PHP script then deals with checking the database/session/etc for updates and the client is only waiting on a "slow" server. This is how twitter implement their various feeds via APIs - each new tweet is returned on a new line as they're generated.

Note that this can be a pain to implement as you're just shifting the polling from the client to the PHP but it does make it more elegant from a JavaScript point of view, removes the delay and reduces wasted network overhead.

这篇关于如何从另一个页面更改页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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