5

I am using below code to keep the radio button selection after a form submission, but it keep resetting to the last button after form submission

<input type="radio" name="button" value="Yes" <?php if(isset($_POST['button']) == 'Yes')  echo ' checked="checked"';?> />Yes
<input type="radio" name="button" value="No" <?php if(isset($_POST['button']) == 'No')  echo ' checked="checked"';?> />No

How can I keep the selection ?

acr
  • 1,674
  • 11
  • 45
  • 77

1 Answers1

12

isset() returns a boolean. As such, comparing directly to Yes/No is not what you want.

What you want is:

if (isset($_POST['button']) && $_POST['button'] == 'Yes')
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174