当尝试使单选按钮粘着时,未定义变量


Undefined Variable when trying to make a radio button sticky

下面是我的代码:

$isYes = FALSE;
$isNo = FALSE;
if (!empty($_POST['owner'])) {
 $o = trim($_POST['owner']);
 if ($_POST['owner'] == 'yes') {
     $isYes = TRUE;
 } else if($_POST['owner'] == 'no'){
     $isNo = TRUE;
     }
 }
<p><label>Is the Applicant the owner? &nbsp;&nbsp;
Yes<input type="radio" name="owner" id="owner_yes" class="owner" value="yes" 
<?php print($isYes ? "CHECKED" : ""); ?> />
No<input type="radio" name="owner" id="owner_no" class="owner" value="no" 
<?php print($isNo ? "CHECKED" : ""); ?>/></label></p>

当用户第一次加载页面时,它给出这个错误:

注意:**未定义变量:isYes在C:'xampp'htdocs'index.php 299行
注意:**未定义变量:isNo在C:'xampp'htdocs'index.php 301行

这是因为用户还没有在上面填充的单选框中选择"是"或"否",但是当他们点击提交并回来修复他们可能搞砸的东西后,错误就消失了,因为$isYes$isNo现在有了一个值。

如何修复这个错误?提前感谢各位!

为什么要设置$isYes=FALSE ?试着

<input type="radio" value="yes" <?php if(isset($isYes)&&!empty($isYes)){echo "checked";}? />Yes
<input type="radio" value="no" <?php if(isset($isNo)&&!empty($isNo)){echo "checked";}? />No

我明白了。我必须这样做:

if (isset($_POST['owner'])) { print($isNo ? "CHECKED" : ""); }

我必须检查变量是否被设置,如果是,那么设置值