Reputation: 335
i have many radio buttons inside foreach loop and it's onclick it goes to it's corresponding url . i want the selected radio button stays checked after page refresh using php or javascript. sorry for my broken english.
<ul>
<?php foreach($companyStatus as $statuses => $status): ?>
<li>
<input
type="radio"
value="<?php echo $url?>"
name="group1"
onclick="if(this.checked){window.location=this.value;}"
><?php echo htmlspecialchars($status['fldStatusName'])?>
</li>
<?php endforeach;?>
</ul>
Upvotes: 1
Views: 3340
Reputation: 2535
Set a cookie with Javascript (onclick of the radio button) and include a script like this:
window.addEventListener('load', function() { READ COOKIE FUNCTION HERE }, false);
See Get cookie by name on how to read a specific cookie. Then, set the radio button according with the state in the cookie.
//edit: some more details
document.getElementById('radiobuttonid').addEventListener('click', function() { SET COOKIE HERE }, false);
(see http://www.w3schools.com/js/js_cookies.asp for how to set a cookie)window.addEventListener('load', function() { READ COOKIE FUNCTION HERE }, false);
The READ COOKIE FUNCTION HERE includes reading the cookie value for the radio button and setting the attribute checked="checked" for it.
To get even more info, you may refer to http://www.somacon.com/p143.php (also some sorcecode, but I didn't check it).
Upvotes: 1
Reputation: 32532
You need to record the radio buttons' selected checked state either in a cookie or as a session variable or in a flatfile or db. Which method is the best is up to you, depends on how long you want your script to "remember" it, how many radio buttons there are to "remember", how "well" you want it to be remembered (eg: using a cookie only would not be the best way for long term because clients can easily delete it)
Upvotes: 0