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

2023-09-06 22:10:45 作者:你许愿我负责实现 お

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

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