2017-04-25 51 views
-3

我有一个名为music的变量。我想如果该变量=没有选中单选按钮,并且如果它=是,则单选按钮被选中。有任何想法吗?这里就是我有这么远,但不工作:PHP变量默认值选中单选按钮

<?php 
$music = $venue['music']; 
echo $music;  
?> 

No<input type="radio" name="music" value="No" <?php $music == 'No' ? 'checked' : '' ?>/> 
Yes<input type="radio" name="music" value="Yes" <?php $music == 'Yes' ? 'checked' : '' ?>/> 
+2

你还需要回显三元组并使用'isset()'。你现在拥有的东西并不多。 –

+0

'<?php echo $ music =='No'? 'checked':''?>' –

+0

@PavloZhukov那么工作非常感谢你!如果你添加它作为答案,我可以标记为正确的:) – sd0093

回答

1

你是不是输出'checked'字符串。请使用echo<?= ?>

例如:

No <input type="radio" name="music" value="No" <?php echo ($music == 'No' ? 'checked' : ''); ?>/> 
Yes <input type="radio" name="music" value="Yes" <?= ($music == 'Yes' ? 'checked' : '') ?>/> 
0
<?php 

$music = $venue['music'];; 

if($music == "Yes" || $music == "yes") 
{ 
    ?> 
    <input type = "radio" checked="checked"> 
    <label>Yes</label> 
    <?php 
}else 
{ 
    ?> 
    <input type = "radio"> 
    <label>No</label> 
    <?php 
} 

?> 

checked="checked"设置你的单选按钮,默认情况下启用

0

你的逻辑是正确的,但你只是丢弃返回 表达式。您需要echo字符串'checked',可以使用 语言构造本身,也可以使用短回波标记<?= ?>。有些 选择:

<input type=" ... " <?php echo $music == 'No' ? 'checked' : null ?> /> 
<input type=" ... " <?= $music == 'No' ? 'checked' : null ?> /> 
<input type=" ... " <?php if ($music == 'No') echo 'checked' ?> /> 
0

你只要把“检查”:“检查”

请找到下面的代码:在我的情况我已经把“是”作为托运:

No <input type="radio" name="music" value="No" <?php echo ($music == 'No' ? 'checked' : ''); ?>/> 
    Yes <input type="radio" name="music" value="Yes" <?php echo ($music == 'Yes' ? 'checked' : 'checked') ?>/>