I tripped on this one.
I have an HTML form that has all kinds of input elements - text, textarea, radio buttons and checkboxes.
I have no problem inserting or updating data in MySQL DB from the HTML form.
But when I want to display what is already in the DB back on my HTML form, it displays the data that is in text and textarea, but not in radio buttons and checkboxes.

Please advise.

Dani AI

Generated

As pointed out, the browser only shows radio buttons or checkboxes as selected when the HTML includes the checked attribute for that specific input. The practical steps are: fetch the stored value(s), normalize them (trim/case), and when you output each input compare its value attribute to the stored value(s). If they match, emit checked="checked" (or just checked for HTML5). asked for examples, so here are concise, safe patterns you can drop into your edit form.

/* radio example (single stored value) */
$gender = isset($row['gender']) ? $row['gender'] : '';

?>
<label><input type="radio" name="gender" value="M" <?php echo ($gender === 'M') ? 'checked="checked"' : ''; ?>> Male</label>
<label><input type="radio" name="gender" value="F" <?php echo ($gender === 'F') ? 'checked="checked"' : ''; ?>> Female</label>
<?php
/* single boolean checkbox */
<input type="checkbox" name="subscribe" value="1" <?php echo (!empty($row['subscribe']) && $row['subscribe']==1) ? 'checked="checked"' : ''; ?>>
/* multiple checkboxes: DB stores "red,green" (or use json_decode for JSON) */
$selected = !empty($row['colors']) ? array_map('strtolower', array_map('trim', explode(',', $row['colors']))) : [];
$options = ['red','green','blue'];
foreach ($options as $color) {
    $is = in_array($color, $selected) ? 'checked="checked"' : '';
    echo '<label><input type="checkbox" name="colors[]" value="'.htmlspecialchars($color,ENT_QUOTES).'" '.$is.'> '.htmlspecialchars($color).'</label>';
}

Troubleshooting and cautions: make sure the stored values exactly match the input value attributes (watch case and whitespace), trim and normalize before comparing, use htmlspecialchars when echoing values to avoid XSS, and prefer prepared statements/PDO when fetching $row from the database. To debug, var_dump($row['field']) to confirm what you actually fetched. Finally, avoid using nonstandard checked="yes" — use checked or checked="checked" for reliable behavior.

Recommended Answers

All 4 Replies

Could you post an example code of what you did, so we can see where you went wrong?

Thanks,
Cody

while displaying the details, u can check the value stored in ur DB against ur checkbox or radio button and when a match occurs just set the attribute as checked="yes"
if u are unable to do this, paste ur code here..

I tripped on this one.
I have an HTML form that has all kinds of input elements - text, textarea, radio buttons and checkboxes.
I have no problem inserting or updating data in MySQL DB from the HTML form.
But when I want to display what is already in the DB back on my HTML form, it displays the data that is in text and textarea, but not in radio buttons and checkboxes.

Please advise.

Actually, I solved this problem earlier today, but too sleepy now to give the details.
I'll do it tomorrow.

Thank you very much!

well in that case u should mark this thread as solved so others wont waste their time to figure out whats wrong...
Hope u agree..

Actually, I solved this problem earlier today, but too sleepy now to give the details.
I'll do it tomorrow.

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.