Greetings,
I need help with preselecting checkboxes in a HTML form.

The first form has several checkboxes and then I post this to a php page which based on the checkboxes selected will send out emails. Each check box corresponds to an email address. After I send an email to selected people, I then at a later point would like to send an update email. The second form should be pulling up information regarding these checkboxes and select boxes accordingly.

The checkbox portion in the form looks like this!

<INPUT TYPE=CHECKBOX NAME=dl[] VALUE=john@email.com>John
<INPUT TYPE=CHECKBOX NAME=dl1[] VALUE=rambo@email.com>Rambo
<INPUT TYPE=CHECKBOX NAME=dl2[] VALUE=everyone@email.com>Everyone

I then post the above to a PHP page, which then inserts these values into mysql database. I use array_merge to merge the values into a string into a database. When inserted, it get inserted as [email],,[/email]. My second form has the same checkboxes but I am clueless regarding reading the checkbox values back and checking the boxes automatically.

Please advise!

Thank you in advance,
- Gravis

Dani AI

Generated

Good approach and here’s a concise, practical workflow that fills the gaps: fetch the previously saved selections as a PHP array, fetch the full list of possible recipients, then generate each checkbox and emit the checked attribute only when that value is present in the saved array. This keeps the edit/update form “sticky” and avoids fragile string parsing in the browser.

Example (server-side generation):

<?php
// $savedJson comes from the DB (prefer JSON or a normalized table)
$saved = json_decode($savedJson ?? '[]', true) ?: [];

// $allRecipients should come from a reliable source (DB or config)
$allRecipients = [
  'alice@example.com' => 'Alice',
  'bob@example.com'   => 'Bob',
  // ...
];

foreach ($allRecipients as $email => $label) {
    $checked = in_array($email, $saved, true) ? ' checked' : '';
    printf(
      '<label><input type="checkbox" name="recipients[]" value="%s"%s> %s</label>',
      htmlspecialchars($email, ENT_QUOTES, 'UTF-8'),
      $checked,
      htmlspecialchars($label, ENT_QUOTES, 'UTF-8')
    );
}
?>

Practical notes and troubleshooting

  • Prefer storing selections as a normalized child table (message_id, recipient) or as JSON in one column. Both are far easier to read back than ad-hoc HTML/BBCode.
  • If you must use CSV, explode() then array_map('trim', ...) before comparisons.
  • Ensure the compared values match exactly (trim whitespace, remove wrappers like mailto: or tags) and use strict comparison when possible.
  • Always escape output with htmlspecialchars() and give labels/inputs unique ids for accessibility.
  • If checkboxes still won’t show as checked, dump the saved array (var_export()/print_r()) to confirm the values are present and in the same format you’re comparing against.

As hinted, the visible “checked” state is just an attribute you emit conditionally; doing that from server-side logic makes the second form reliably reflect the saved choices.

Use the "checked" option.

<INPUT TYPE=CHECKBOX NAME=dl[] checked VALUE=john@email.com>John

You'll likely want to bookmark this page:
http://www.w3.org/TR/REC-html40/interact/forms.html

A couple notes:
1. Always put your HTML options in quotes. If you get a space in the input field somewhere, it will interpret it up to the space.
2. Take care in placing the boxes to avoid ambiguity and save your users some trouble. Don't check them all "for" the user if the user is likely to uncheck them all.

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.