2017-04-25 48 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') ?>/>