简体   繁体   English

切换变量以更改href

[英]Toggle variable to change href

In my footer.php I want to give people the option to change the language from english to german. 在我的footer.php中,我想让人们选择将语言从英语更改为德语。 I want to do this manually, because I have a lot of external links, so I do not want to use plugins since it should be easy with php. 我想手动执行此操作,因为我有很多外部链接,所以我不想使用插件,因为使用php应该很容易。

All I need to do is to change the href and the content between the anchor. 我需要做的就是在锚点之间更改href和内容。

<?php 
  session_start();
  if($_SESSION["language_is_german"])
  {
    echo "<a href='www.example.com?lang=en'>EN</a>";
    $_SESSION["language_is_german"] = true;
  }
  else
  {
    echo "<a href='www.example.com?lang=de'>DE</a>"
    $_SESSION["language_is_german"] = false;
  }
?>

This is the way I thought it might work, but it doesn't.. 这是我认为可能会起作用的方式,但是没有。

You cannot set PHP Variables in JavaScript: 您不能在JavaScript中设置PHP变量:

onclick="$language_is_german = true"

The above code is not valid. 上面的代码无效。 You need to use Sessions for this! 您需要为此使用会话! Or you need to use JavaScript for this. 或者您需要为此使用JavaScript。 If you are using PHP, you can do something like this: 如果您使用的是PHP,则可以执行以下操作:

session_start();
$_SESSION["language_is_german"] = false;

And in the setting PHP, you can do something like this: 在设置PHP中,您可以执行以下操作:

session_start();
$_SESSION["language_is_german"] = true;

Finally, your if has to be this way: 最后,您的if必须是这样的:

<?php 
    if($_SESSION["language_is_german"])
       echo "<a href='www.example.com?lang=en'>EN</a>";
    else
       echo "<a href='www.example.com?lang=de'>DE</a>"
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM