Mike Preble
Mike Preble

Reputation: 155

php filling in selected in a dynamic list

My question extends on the answer on this post and I couldn't find what I'm looking for posted otherwise.

How do I auto select (checked) options in a checkbox list when the options are being pulled from a mySQL table (dynamic)?

Here's what I have so far:

Pulling the committees (comms) from the table:

while ($row = mysql_fetch_assoc($comm)){
    foreach ($row as $v){
        $comms[] = $v;
        }
}

outputting pretty HTML with tidy PHP (the part I need help with):

    foreach ($comms as $comm){
       ?????????
    }

How can I output something like?:

<input type="checkbox" name="committee" value="blue" checked="checked" />blue<br />
<input type="checkbox" name="committee" value="green"  />green<br />
<input type="checkbox" name="committee" value="orange"  />orange<br />
<input type="checkbox" name="committee" value="purple" checked="checked" />purple<br />

Upvotes: 0

Views: 361

Answers (3)

Matt
Matt

Reputation: 7040

Assuming your colors are stored in an array and your data comes back as an array (likely not the case if you're using mysql_* functions), you can print out each checkbox, then compare its value to see if it's in the list of "checked" colors:

<?php
$colors = array('blue', 'green', 'orange', 'purple');

$data = array('blue', 'purple');//array pulled from database.

foreach($colors as $color): ?>
    <input type="checkbox" name="committee" value="<?= $color ?>" <?= in_array($color, $data) ? 'checked="checked" ' : '' ?>/><br />
<?php endforeach; ?>

By the way, you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.

Upvotes: 1

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

while ($row = mysql_fetch_assoc($comm)){
    foreach ($row as $v){
        $comms = $v;
        }
}

where you can print values from assuming that $v has an attribute Checked

   foreach($coms as $key => $value)
   {
      $checked = "";
      $color = $value['color'];
      if($value['checked'] == 'checked')
      $checked = "checked = 'checked'";
      echo "<input type='checkbox' name='committee' value='$color' $checked />$color<br />";

}

Upvotes: 0

andrewsi
andrewsi

Reputation: 10732

There are a couple of things you need to check. If someone's tried to submit the form already, then their responses will be in $_REQUEST (and $_POST or $_GET, depend on your form's submission method); you can then write each line as:

<input type="checkbox" name="committee[]" value="green" <?php if ($_REQUEST['committee'] == 'green') { echo "checked"; } ?> />green<br />

If it's coming from the database, you can check in $comms"

<input type="checkbox" name="committee[]" value="green" <?php if (in_array($comms, 'green') { echo "checked"; } ?> />green<br />

You can make the code more efficient by working out in advance whether it's a re-submission or not, and populating a single array with the result from the right place:

<?php

if ([form is submitted]) {
    $comms = $_REQUEST["committee"];
} else {
    $comms = getCommsFromDatabase();
}

?>

That also gives you the chance to sanitise the input from $_REQUEST.

Upvotes: 0

Related Questions