two Qs in one day…
I’m trying to get the right radio button checked when I pull data from a mysql database to display in an edit form. Have tried all kinds of code but get the same result each time: the last value is always the one checked. Mystified.
My code is somewhat complicated by being inside a long html string, hence the construction for reading the php variables. I have this code at the top of the page:
$row = mysql_fetch_assoc($result, MYSQL_ASSOC);
if ($row)
{
print("<p>$se_id is in the database. You can edit this record.</p>");
//get variables to set radio buttons in $form
$type = $row['se_source'];
$checked[$type] = "checked";
etc… and this code inside the html string:
<li>
<label for="se_source">Contributed by:</label>
echo ' . $type . '
<input id="se_source" type="radio" id = "radio" name="se_source" value="CNFP" checked = ' . $checked["CNFP"] . '>CNFP</input>
<input id="se_source" type="radio" id = "radio" name="se_source" value="User" checked = ' . $checked["User"] . '>User</input>
The echo statement is for my own sanity — I know it’s not really an echo here. But even when it says “echo CNFP” it is always the “User” button that is checked. So I know it is getting the correct value for that variable, but for some reason it defaults to the last value in the list. I’ve tried it with “===” as well. No joy. Any enlightenment appreciated, thanks so much for taking a look.
AFAIK, the value of the checked attribute isn’t important, only the presence of it is sufficient to mark the checkbox as checked.
(HTML5 standard confirms this)
Try something like this: