Ok,
I have two radio buttons 'A' and 'B' and user have a choice to select anyone of them,
by default button 'A' is selected but when user come to my website and selects 'B' then selection changes to button 'B' however the problem is,

when the user move to another page in my site the selection goes back to default :S ??
How to fix it ??
can it be done with the help of cache and session ? if yes then how :?

here is the buttons,

<form name="f1" method="POST" action="<?php echo $PHP_SELF;?>">
<span>Family filter:</span>
<ul>
<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_POST['r1'] == 'o') ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_POST['r1'] == 'p') ? 'checked="checked"' : ''; ?> />Off</li>
</ul>
</form>

I tried this but doesn't work,

<form name="f1" method="POST" action="<?php echo $PHP_SELF;?>">
<span>Family filter:</span>
<ul>
<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo ($_SESSION['r1'] == "o") ? 'checked="checked"' : ''; ?><?php echo ($_POST['r1'] == 'o' ) ? 'checked="checked"' : ''; ?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo ($_SESSION['r1'] == "p") ? 'checked="checked"' :''; ?><?php echo ($_POST['r1'] == 'p') ? 'checked="checked"' : ''; ?> />Off</li>
</ul>
</form>

and the Top,

<?php 
session_start(); 
$r1= $_POST["r1"]; 
session_register("r1"); 
?>
Member Avatar for diafol

You're taking about a radiobutton selection NOT a button, right?

So you want the system to remember the last selection, even if you go away from the page and come back later?

Here's an example (not tested):

session_start();
if(isset($_POST['r1'])){
  //if form posted - you can add another check - defaults to 'p' if value not 'o'
  $r1 = ($_POST['r1'] == 'o') ? array(' check="check"','') : array('',' check="check"');
  $_SESSION['r1'] = $r1; 
}elseif(isset($_SESSION['r1'])){
  //get last remembered setting if returning
  $r1 = $_SESSION['r1'];
}else{
  //set default values for first time
  $r1 = array('',' check="check"');
  $_SESSION['r1'] = $r1;
}
//you could add a setcookie() for getting $_COOKIE as well if you want to save it past the session expiry.


<form name="f1" method="post">
<span>Family filter:</span>
<ul>
<li><input type="radio" name="r1" value="o" onClick="submit();" <?php echo $r1[0];?> />On</li>
<li><input type="radio" name="r1" value="p" onClick="submit();" <?php echo $r1[1];?> />Off</li>
</ul>
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.