提交表单后保持单选按钮处于选中状态

2022-01-21 00:00:00 radio-button forms php

我使用下面的代码在表单提交后保持单选按钮选择,但在表单提交后它一直重置到最后一个按钮

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

如何保留选择?

推荐答案

isset() 返回一个 boolean.因此,直接与 Yes/No 进行比较不是您想要的.

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

你想要的是:

if (isset($_POST['button']) && $_POST['button'] == 'Yes')

相关文章