简体   繁体   中英

javascript pass value to another page

I have a php program with two different lists of categories. I am using radio buttons with javascript to let the user toggle between the two lists. 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. 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. Is there a way to do this in Javascript? (hopefully, without getting into cookies)

Despite your statement to receive a JS-solution, I hope you don't mind me suggesting you using a PHP-session for this:

It needs to be initialized in every affected PHP-file with the command

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. :) Also remember that this is with absolutely no data-safety-guarantee as $_POST-data should never be processed unescaped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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