Html 传递php变量但没有加载新页面

时间:2021-09-29 17:07:48

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.

我正在一个网站上,用户可以通过页面顶部的链接更新主题。免责声明:稍后我将用Javascript替换链接,但是现在我想要一个安全备份,以防用户不支持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.

我可以通过我的PHP获得链接显示得很好。 如果它是浅色主题,则显示为如果用户使用黑暗主题。

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.

但是,单击这些链接中的任何一个似乎只更新我的地址栏的浏览器,强制我手动刷新页面以更改主题。单击链接时页面不会重新加载,因此主题不会更改。它的行为类似于异步请求,但相关的更改都是嵌入在HTML中的PHP,因此它们在刷新之前不会发生。

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.

使用上面的代码点击链接将我带到/index.php?theme=theme,但我必须在更改保持之前刷新。没有从服务器加载新的html,因此body ID不会更改。

Cookie stays intact.

Cookie保持不变。

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.

附:就像我说我将使用Javascript制作异步版本,我只想首先支持没有启用它的用户。谢谢。

1 个解决方案

#1


3  

Change this line

改变这一行

<body id="<?php
echo $_COOKIE["theme"];
?>">

to this

对此

<body id="<?php
echo $theme;
?>">

and this

和这个

if ($_COOKIE["theme"]=="light")

to this

对此

if ($theme=="light")

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.

您的问题是,当您在当前页面加载中设置cookie时,在下一页加载之前它不可用。因此,为什么刷新似乎使它工作。

#1


3  

Change this line

改变这一行

<body id="<?php
echo $_COOKIE["theme"];
?>">

to this

对此

<body id="<?php
echo $theme;
?>">

and this

和这个

if ($_COOKIE["theme"]=="light")

to this

对此

if ($theme=="light")

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.

您的问题是,当您在当前页面加载中设置cookie时,在下一页加载之前它不可用。因此,为什么刷新似乎使它工作。