保存单选按钮状态php


Save radio button status php

表单表是在echo中引入的,对于文本字段,我使用value=" ' .$_POST['name']来设置默认值,如果表单已经发送了至少一次。它工作正常。但是,当表单已经发送时,我如何保存单选按钮状态?谢谢。

<tr>
<td colspan="2" align="left" valign="top"> <input type="radio" name="ambiente" value="si" />
         Si
<input type="radio" name="ambiente" value="no" />
       No</td>
</tr>

简体:

<input type="radio" name="ambiente" value="si" <?php if ($_POST['ambiente'] == 'si') echo 'checked'; ?> /> Si
<input type="radio" name="ambiente" value="no" <?php if ($_POST['ambiente'] == 'no') echo 'checked'; ?> /> No
<?php
   $checked = NULL;
    if(isset($_POST['ambiente']) && $_POST['ambiente'] == 'no') {
        $checked = 'checked="checked"';
    }
?>
<input type="radio" name="ambiente" value="no" <?php echo $checked ?> />

或者可以在一行中显示:

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