简体   繁体   English

javascript将值传递给另一个页面

[英]javascript pass value to another page

I have a php program with two different lists of categories. 我有一个带有两个不同类别列表的php程序。 I am using radio buttons with javascript to let the user toggle between the two lists. 我正在使用带有javascript的单选按钮,以使用户在两个列表之间切换。 The default is the first button but, if he hits the second button, he toggles to the second list and, if he hits the first button, he can go back to the first list. 默认值为第一个按钮,但是,如果他按了第二个按钮,则切换到第二个列表,如果他按了第一个按钮,则可以返回第一个列表。

<input type="radio" name="catmfg" value="0" checked onclick="document.getElementById('mfg').style.display='none', document.getElementById('cat').style.display='block'" /><b>Browse Categories</b><br/>
<input type="radio" name="catmfg" value="1" onclick="document.getElementById('cat').style.display='none', document.getElementById('mfg').style.display='block'" /><b>Browse Manufacturers</b><br/>

It works fine but the problem is that this list is a navigation sidebar on every page and, since 'cat' is the default, if the user chooses the 'mfg' list and clicks on a link to go to a detail page, he sees the 'cat' sidebar on the detail page. 它工作正常,但问题在于此列表是每个页面上的导航侧栏,并且由于默认为“ cat”,因此如果用户选择“ mfg”列表并单击链接以转到详细信息页面,他会看到详细信息页面上的“猫”侧边栏。 I want him to see the same list he initially chose on the next page he goes to. 我希望他在他转到的下一页上看到他最初选择的列表。 If he chose the 'mfg' list, I want him to see that list on the detail page. 如果他选择了“ mfg”列表,我希望他在详细信息页面上看到该列表。 Is there a way to do this in Javascript? 有没有办法用Javascript做到这一点? (hopefully, without getting into cookies) (希望不会进入Cookie)

Despite your statement to receive a JS-solution, I hope you don't mind me suggesting you using a PHP-session for this: 尽管您声明要获得JS解决方案,但我希望您不要介意我建议您为此使用PHP会话:

It needs to be initialized in every affected PHP-file with the command 需要使用以下命令在每个受影响的PHP文件中对其进行初始化:

session_start() 

before any output starts. 在任何输出开始之前。 Then, where the values of the form are received, use eg 然后,在收到表单值的地方,使用例如

$_SESSION['param']['catmfg'] = $_POST['catmfg'];

Later, you recall your stored value with eg: 以后,您可以使用以下方法来调出储值:

if(isset($_SESSION['param']['catmfg']) && !is_null($_SESSION['param']['catmfg])
{
<feed something with the value from $_SESSION['param']['catmfg'] here>
}

I hope this comes in helpful for you as this is also my first ever answered question here on StackOverflow. 我希望这对您有所帮助,因为这也是我在StackOverflow上第一个回答的问题。 :) Also remember that this is with absolutely no data-safety-guarantee as $_POST-data should never be processed unescaped. :)还请记住,这绝对没有数据安全保证,因为$ _POST-data绝对不能未经转义处理。

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

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