Html 传递 php 变量但不加载新页面
我正在开发一个网站,用户可以通过页面顶部的链接更新主题。免责声明:稍后我将用 Javascript 替换该链接,但现在我想要一个安全的备份,以防用户不支持 Javascript。
我可以用我的 PHP 来很好地显示链接。如果是浅色主题,则显示为 并显示为
如果用户使用深色主题。
但是,单击这些链接中的任何一个似乎只会更新地址栏的浏览器,迫使我手动刷新页面以更改主题。当我单击链接时,页面不会重新加载,因此主题不会更改。它的行为类似于异步请求,但相关更改都是嵌入在 HTML 中的 PHP,因此只有在刷新时才会发生。
我做错了什么吗?当我点击这些链接时,有没有办法强制页面重新加载?
感谢您提供的任何帮助!
编辑:这是当前的脚本:
<?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);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<!-- head content... -->
</head>
<body id="<?php
echo $_COOKIE["theme"];
?>">
<div id="container">
<div id="header">
<a id="switch" href="?theme=<?php
if ($_COOKIE["theme"]=="light")
echo "dark";
else
echo "light";
?>">Lights</span>
</div>
<!-- Rest of page content -->
</div>
</body>
</html>
使用上面的代码,单击链接会将我带到 /index.php?theme=theme,但我必须在更改生效之前刷新。没有从服务器加载新的 html,因此 body id 不会改变。
Cookie 保持完好无损。
PS 就像我说过的,我将使用 Javascript 制作一个异步版本,我只是想首先支持未启用它的用户。谢谢。
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.
I can get the link to display just fine with my PHP. It comes out to <a href="?theme=dark">
if it is the light theme and it displays as <a href="?theme=light">
if the user is using the dark theme.
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.
Am I doing something wrong? Is there a way to force the page to reload when I click these links?
Thanks for any help you can offer!
EDIT: Here's the current script:
<?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);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<!-- head content... -->
</head>
<body id="<?php
echo $_COOKIE["theme"];
?>">
<div id="container">
<div id="header">
<a id="switch" href="?theme=<?php
if ($_COOKIE["theme"]=="light")
echo "dark";
else
echo "light";
?>">Lights</span>
</div>
<!-- Rest of page content -->
</div>
</body>
</html>
With the above code clicking the links takes me to /index.php?theme=theme, 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.
Cookie stays intact.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此行更改
为此,
并将此更改
为此
您的问题是,当您在当前页面加载中设置 cookie 时,它在下一个页面加载之前不可用。这就是为什么刷新似乎让它起作用。
Change this line
to this
and this
to this
Your issue is that when you set a cookie in the current page load, it isn't available until the next page load. hence why refreshing seems to make it work.