How can I send a message or data to an already open other window?

I am just starting to learn PHP. I am developing a shopping cart. I have Add to Cart buttons on each of my product pages and I can launch the shopping cart in a new browser tab.

The HTML for the Add to Cart button is here :

Now if I want to add a second instance of this product, or even add a new product from another web page to the cart, I naturally get a new tab again (due to the target="_blank" attribute of the form) with the new product added. But I don’t want to get a new shopping cart tab every time I add something to it, I want the original shopping cart tab to be updated.

So somehow I need to pass data from the other product web page Add to Cart button to the server but have the server send the resulting shopping cart page to the already open shopping cart.

How can I do this?

PayPal does this somehow but I do not have enough web programming experience to figure out how it is done.

Thanks very much.

This is not a trivial question at all, and there are multiple ways to do it. Before I answer, I’ll need a bit of extra detail from you:

  • Are your two frames in a child-parent relationship? In other words, are we dealing with two separate windows or an iframe within a window? This is absolutely crucial in how you are going to solve this, as window<->window communication is not directly possible, while iframe<->window is possible only if the two are within the same sandbox (same domain, same protocol, same port)
  • Are you dealing with only two frames? In other words, if you click the Add Cart button, do you want all the open copies of your site to update, or just one?
  • I’ll also need to know how familiar you are with JavaScript, as all solutions will use it, and one of them will use a loophole present in all browsers. If you are not, it’s not a problem - tell me and I’ll explain every step in even more detail

If the answer to the first one is iframe-window and the second is just one window, you can get away with pure JS. Every other method will require some form of server-side process.

Thanks for your quick reply.

I haven’t quite decided yet whether to popup the shopping cart in an iframe in the same browser window as the product Add to Cart button, or to display it in a separate tab. PayPal does the latter, whereas the ecwid solution does the former. I’m inclined to use a separate tab but may fall back to the inline frame method later once I can figure out how to do it.

Whilst I am relatively new to web programing I have a20 years experience in C, C++ and C# development in both Unix and Windows .NET environments. I have some JavaScript experience but not much. But once you have learned C all the other languages that have similar syntax are very easy to pick up, including Javascript. The one language I absolutely abhor is VB - it is so verbose!

I appreciate any help you can give me.

More thoughts - I’m going to need a solution for messaging to another web page since every product on my site has its own web page with its own Add to Cart button. So any page can create the shopping cart first. Then any subsequent page that has its Add to Cart button clicked must cause the first shopping cart instance to be updated.

To user sebrenauld. Have you had any luck in coming up with a solutions fro cross page udating?

I am now using a hidden div panel in each product’s web page to host the shopping cart when its View Cart or Add to Cart buttons are clicked. so for that particular product, they can view or close the cart within that page. However when another product’s web page Add to Cart button is clicked I want its own Cart to remain hidden and the first (and only) cart to be updated. This is what commercially available shopping carts do - they even make the tab for that page flash!

Now you might ask, why am I re-inventing the wheel and developing my own cart when there are so many already out there? Well, the free ones either haven’t got the functionality I need, and/or look awful, and the commercial ones both cost too much and they also don’t have the advanced shipping cost calculator I need, so I am rolling my own.

I have no problem with JavaScript and PHP. The server backend will PHP and MySQL.

I wondered about AJAX. If, when a particular user first creates a new order by adding the first item to his empty cart I go to the database and generate a new order number with order status of pending or open. Then generate a cookie containing that order number. Then any browser window e.g. from another product (all on the same domain and same web site), could access that cookie to retrieve the order number, then we could go to the database to get the existing order contents. In the mean time any existing shopping cart that is visible could periodically make an asynchronous call to the server, say once per second, to get the update cart contents. When the user closes that cart, the asynchronous polling would be stopped.

Would this work and would it pose a serious security risk?

Sponsor our Newsletter | Privacy Policy | Terms of Service