问题
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 ?
回答1:
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')
来源:https://stackoverflow.com/questions/18450998/keep-radio-button-selected-after-a-form-submit