I have a variable called music. I would like if that variable = no the no radio button is checked and if it = yes the yes radio button is checked. Any ideas? Here's what I have so far but isn't working:
<?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' : '' ?>/>
You are not outputting the 'checked'
string. Either use echo
or <?= ?>
.
For example:
No <input type="radio" name="music" value="No" <?php echo ($music == 'No' ? 'checked' : ''); ?>/>
Yes <input type="radio" name="music" value="Yes" <?= ($music == 'Yes' ? 'checked' : '') ?>/>
<?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"
sets your radio button to enabled by default
Your logic is correct, but you're just discarding the returned expressions. You need to echo
the string 'checked'
, either with the language construct itself or with the short echo tags <?= ?>
. Some alternatives:
<input type=" ... " <?php echo $music == 'No' ? 'checked' : null ?> />
<input type=" ... " <?= $music == 'No' ? 'checked' : null ?> />
<input type=" ... " <?php if ($music == 'No') echo 'checked' ?> />
You just put "checked" : 'checked'
Please find the code below: in my case i have put 'yes' as checked:
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') ?>/>