How to reload page, rerun php code – PHP – SitePoint

Hi, I have a web site with a couple of menu options / links.
All the pages are essentially the same except for a php loop that selects different content from a json file, so was wondering how to reload one page and just run different php code according to menu option selected.
Thanks for any help

It depends on what kind of links they are. If they’re normal <a> links, you could stick a code on the end of the URL, and when you render the page, provide the content appropriate to that code. If it’s a <select>, you could use Javascript to trap the onChange() event, call a small script to get the different content, and insert it directly into your document.

JavaScript is used to modify pages in the browser without making another round trip to the server. You can significantly reduce and even eliminate round trips to the server completely by creating a SPA rather than a traditional server rendered series of strung together web pages. The legacy way of doing this is to piece mail partial html fetched from the server and add to the page using JavaScript. However, there are an overwhelming amount of disadvantages to that approach at scale. Thus the rise of the modern MVVM (model-view-view-model) innovations; React, Vue, and Angular. React and Vue are slim built to be easily dropped into an existing website such as yours probably. Web experiences that eliminate trips to the server are also more environment friendly. Think of it as conserving energy unnecessary to consume using modern tools and methodologies.

Thanks very much for this, I’m new to PHP & Javascript so I’m not clear how to implement your solutions.
The links I have are:

 <a class='mnuitem' href="index.php">Oil on <br> Canvas</a> <a class='mnuitem' href="acrylic.php">Acrylic, Pencil,<br/>Water Color</a>

And the php code that I need to change

 if ($entry['medium'] == "Oil on canvas") {

to

 if ($entry['medium'] == "Acrylic") {

I would like to use just index.php and not have to redirect to acrylic.php. Any snippets on how I can do this much appreciated.

Thanks

The easiest, if you can change things a little, would be to modify your links to be:

<a class='mnuitem' href="index.php?medium=oil">Oil on <br> Canvas</a>
<a class='mnuitem' href="index.php?medium=acrylic">Acrylic, Pencil,<br/>Water Color</a>

And then your menu code:

 if (strtolower ($_GET['medium']) == "oil") {

Leave a Reply

Your email address will not be published.