Note that there are some explanatory texts on larger screens.

plurals
  1. POHtml <a> passing php variables but not loading new page
    primarykey
    data
    text
    <p>I'm working on a website where the user can update the theme by a link at the top of the page. Disclaimer: Later I'll replace the link with Javascript, but for now I want a safe backup in case the user doesn't support Javascript.</p> <p>I can get the link to display just fine with my PHP. It comes out to <code>&lt;a href="?theme=dark"&gt;</code> if it is the light theme and it displays as <code>&lt;a href="?theme=light"&gt;</code> if the user is using the dark theme.</p> <p>However, clicking either of these links appears to only update the browser of my address bar, forcing me to refresh the page manually to change the theme. The page doesn't reload when I click the link, and therefore the theme does not change. It's acting like an asynchronous request, but the relevant changes are all PHP embedded in the HTML, so they don't occur until a refresh.</p> <p>Am I doing something wrong? Is there a way to force the page to reload when I click these links?</p> <p>Thanks for any help you can offer!</p> <p>EDIT: Here's the current script:</p> <pre><code>&lt;?php if ($_GET["theme"]) { $theme = $_GET["theme"]; setcookie("theme", $theme, time()+21*24*60*60); } else if (isset($_COOKIE["theme"])) $theme = $_COOKIE["theme"]; else { $theme = "light"; setcookie("theme", $theme, time()+21*24*60*60); } ?&gt; &lt;!DOCTYPE HTML&gt; &lt;html&gt; &lt;head&gt; &lt;!-- head content... --&gt; &lt;/head&gt; &lt;body id="&lt;?php echo $_COOKIE["theme"]; ?&gt;"&gt; &lt;div id="container"&gt; &lt;div id="header"&gt; &lt;a id="switch" href="?theme=&lt;?php if ($_COOKIE["theme"]=="light") echo "dark"; else echo "light"; ?&gt;"&gt;Lights&lt;/span&gt; &lt;/div&gt; &lt;!-- Rest of page content --&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>With the above code clicking the links takes me to /index.php?theme=<em>theme</em>, but I have to refresh before the changes take hold. There is no new html loaded from the server, so the body id does not change.</p> <p>Cookie stays intact.</p> <p>P.S. Like I said I will be making an asynchronous version with Javascript, I just want to first support users that don't have it enabled. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload