Radio Button Question (probably very simple)

Discussion in 'HTML & Website Design' started by hammurabbi, Feb 8, 2006.

  1. #1
    My "index.php" contains a simple HTML form which contains two radio buttons and a submit button. One radio button is checked by default. Thus.

    <form action="index.php" method="post">
    	<input type="radio" name="choice" value="choice1" checked> Choice 1
    	<input type="radio" name="choice" value="choice2"> Choice 2
    	<input type="submit" name="submit" value="Submit">
    </form>
    Code (markup):
    In other words, the page calls itself, passing the parameter "choice". A script shows content depending on what radio was selected. That all works.

    My problem: When the page loads, the form always automatically checks the default radio button (choice1 in the above example) rather than the one the user just selected.

    Is there a simple way to make the checked radio button default to choice1 on the first page load, then to whatever the user picked on subsequent reloads?

    I've searched for an hour, but can't find any answers.
     
    hammurabbi, Feb 8, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <form action="index.php" method="post">
    	<input type="radio" name="choice" value="choice1" <?php echo (@$_POST['choice']=='choice2')?'':'checked'; ?>> Choice 1
    	<input type="radio" name="choice" value="choice2" <?php echo (@$_POST['choice']=='choice2')?'checked':''; ?>> Choice 2
    	<input type="submit" name="submit" value="Submit">
    </form>
    
    Code (markup):
     
    exam, Feb 8, 2006 IP
  3. hammurabbi

    hammurabbi Peon

    Messages:
    166
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That got me on the right track, thanks a lot for the speedy response!
     
    hammurabbi, Feb 8, 2006 IP