简体   繁体   English

是否可以使用href ='javascript:history.go(-1)通过“返回”按钮发送变量

[英]Is it possible to send a variable with my “Go Back” button using href='javascript:history.go(-1)'

I'm using the following code and would like to send a variable back to the previous page when clicked: 我正在使用以下代码,并且在单击时想将变量发送回上一页:

echo "<center> Photo #{$Count} | <a href='javascript:history.go(-1)'>Go Back</a></center>"

I thought I could do something like this but it doesn't work: I added ?VP='Y' 我以为可以做这样的事情,但是不起作用:我加了?VP ='Y'

echo "<center> Photo #{$Count} | <a href='javascript:history.go(-1)'?VP='Y'>Go Back</a></center>"

Since you tagged PHP, you could do 既然标记了PHP,就可以

echo "<center> Photo #{$Count} | <a href='".$_SERVER['HTTP_REFERER']."?VP=Y'>Go Back</a></center>";

Then on the other page 然后在另一页上

$var = $_GET['VP'];

Would this work? 这行得通吗?

<a href='' id="back">Go Back</a>
<script>
document.getElementById("back").href = document.referrer + "?variable=value";
</script>

Note that document.referrer will be empty if the user navigated to the page directly. 请注意,如果用户直接导航到页面,则document.referrer将为空。 Of course thats a problem, why maybe this would be a back up solution: 当然,这就是一个问题,为什么这可能是一个备份解决方案:

<a href='' id="back">Go Back</a>

<script>
var ref = document.referrer;

if(ref == "") {
    ref = "javascript:history.go(-1);";
} else {
    ref = ref + "?variable=value";
}

document.getElementById("back").href = ref;
 </script>

I see you are using php, so you could solve it by making a simple form with the $_SERVER['HTTP_REFERER'] as the action and add a hidden field with the value. 我看到您使用的是php,因此您可以通过以$_SERVER['HTTP_REFERER']作为操作的简单形式并添加带有值的隐藏字段来解决此问题。 Or add a session variable :) SESSION['yourvar'] = 'whatever' 或添加一个会话变量:) SESSION['yourvar'] = 'whatever'

Then on the next page you get it using the $_GET or $_POST from the form or get the session variable by echo $_SESSION['yourvar'] . 然后在下一页上,使用$_GET$_POST从表单中获取它,或通过echo $_SESSION['yourvar']获取会话变量。 When working with sessions make sure you start them at the start of your pages. 使用会话时,请确保在页面的开头启动会话。 More details here http://php.net/manual/en/book.session.php 更多详细信息,请参见http://php.net/manual/zh/book.session.php

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

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