braumer
braumer

Reputation: 85

AJAX: dynamically change contents of second page

I am attempting to change contents of a second page depending on changes in another page. Simply put i have an image display page - #1 and an information display page - #2.

Page #1 displays an image depending on a get variable which i acquire with javascript function and another function sends it off to a php page with an XMLHTTPRequest and handles it.

Now my question is do I need to use a Database table to store the variable and then create another PHP script acting as a listener from the database and if the variable changes send it off to Page #2 with the display information or is there a simpler way?

In simpler terms - The get variable is set to 1 -> display picture 1 AND show information related to product 1 on PAGE #2. Then get variable set to 2, then 3 and so on...

Thank you!

Upvotes: 1

Views: 81

Answers (1)

Vince
Vince

Reputation: 544

4 options:

1 - You have a PHP website and go ahead as you describe:

Just reuse the parameter received by the first page (www.mysite.com/page1.php?id=1) to load the second one. Your script can take the parameter and modify the link for the second page so that you have www.mysite.com/page2.php?id=1 and on page 2 you know how to get your parameter and use it.

2 - You have a PHP website but you can make things a lot simpler:

The link www.mysite.com/page1.php?id=1 makes your PHP code to intercept the parameter directly so that you load your content directly there and make the link for the second page (www.mysite.com/page2.php?id=1) from the server or PHP and this is sent to the client directly. You don't need so much JavaScript and AJAX, your second page is a PHP page again, so you load everything from the server again. It's the most common way. I hope you don't have any obstacles with this method.

3 - You have a single page application in JavaScript:

If your JavaScript gets the id, just keep the value in a variable and the "second page" would access this variable too.

4 - If your second page is already open and you want it to update when the first one gets modified, YES you need to keep the variable on your server and why not a database. And you need ajax repetitively called with setInterval() (or maybe better with setTimeout() ) to get the variable from a PHP script that will query your database. I would not advise you to make your second page to refresh, it is unpleasant for the user.

There are potentially some other cases if you have other restrictions. Don't hesitate to ask for some details if something is not clear.

Upvotes: 1

Related Questions