Keep radio button selected after a form submit

forms, php, radio-button

Solution

`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')

Problem

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 ?

Original source