Marny A. López Marny A. López - 24 days ago 6
PHP Question

How to auto select a radio button with PHP on HTML

I have this code:

<tr align="center">
<td align="center">
<div class="column column2"><label><input type="radio" name="j" value="Pre-Quarantine" checked="<? if ($dato['Status'] == 'Pre-Quaratine'){ echo true; } ?>"/><span>Pre-Quarantine</span></label></div><span class="clearfix"></span>
</td>
<td align="center">
<div class="column column2"><label><input type="radio" name="j" value="Quarantine" checked="<? if ($dato['Status'] == 'Quarantine'){ echo true; } ?>"/><span>Quarantine</span></label></div><span class="clearfix"></span>
</td>
<td align="center">
<div class="column column2"><label><input type="radio" name="j" value="Released" checked="<? if ($dato['Status'] == 'Released'){ echo true; } ?>"/><span>Released</span></label></div><span class="clearfix"></span>
</td>
</tr>


What I want is to autoselect a radio button based on the value of my database field... But it isn't really working, I would really appreciate if someone could help me :)

Answer

The checked attribute causes the element to be checked if it is present. You don't need to use checked="0/1".

Try this:

<tr align="center">
    <td align="center">
        <div class="column column2"><label><input type="radio" name="j" value="Pre-Quarantine" <? if ($dato['Status'] == 'Pre-Quaratine'){ echo 'checked'; } ?> /><span>Pre-Quarantine</span></label></div><span class="clearfix"></span>
    </td>
    <td align="center">
        <div class="column column2"><label><input type="radio" name="j" value="Quarantine" <? if ($dato['Status'] == 'Quarantine'){ echo 'checked'; } ?> /><span>Quarantine</span></label></div><span class="clearfix"></span>
    </td>
    <td align="center">
        <div class="column column2"><label><input type="radio" name="j" value="Released" <? if ($dato['Status'] == 'Released'){ echo 'checked'; } ?> /><span>Released</span></label></div><span class="clearfix"></span>
    </td>
</tr>