tags:

views:

38

answers:

3

I am creating an update form where previously submitted information from a mysql database is grabbed to populate the current form. So this is what I have so far:

$select = mysql_query("SELECT * FROM some_table WHERE id = $id");
while ($return = mysql_fetch_assoc($select)) {
    $name = $return['name'];
    $bio = $return['bio'];
    $maritalStatus = $return['marital_status'];
    $favFood = $return['fav_food'];
}

<form action="page.php" method="post">
Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br />

Bio: <textarea name="bio"><?php echo $bio; ?></textarea><br />

Marital Status
<select name="maritalStatus">
   <option>Select One</option>
   <option value="married">Married</option>
   <option value="single">Single</option>
   <option value="divorced">Divorced</option>
</select><br />

Favorite Food:
Cheeze: <input type="checkbox" name="favFood" value="cheeze" />
Cake: <input type="checkbox" name="favFood" value="cake" />
Oranges: <input type="checkbox" name="favFood" value="oranges" />
</form>

As you can see I am able to display data that was entered via an input text box or textarea just fine. But How to I have the "Marital Status" drop down preselected to the "divorced" option and the "Oranges" check box checked under "Favorite Food" GIVEN that those two choices are the ones that actually exist in the database?

A: 

To preselect an option within in select you have to set the selected attribute like this:

<option value="married"<?= $marital_status == "married" ? ' selected="selected"' : ''?>>Married</option>
<option value="single"<?= $marital_status == "single" ? ' selected="selected"' : ''?>>Single</option>
<option value="divorced"<?= $marital_status == "divorced" ? ' selected="selected"' : ''?>>Divorced</option>

UPDATE Or more DRY:

<?php foreach(array("married","single","divorced") as $status)): ?>
  <option value="<?php= $status ?>"<?php $martial_status == $status ? ' selected="selected"' : '' ?>>
    <?php= ucwords($status) ?>
  </option>
<?php endforeach; ?>
jigfox
Tis fine only if he has short tags on.
@MrXexxed: I've updated my answer
jigfox
How could I have the "married", "single", "divorced" populate dynamically from the database? Suppose those 3 options weren't hard coded but existed in a separate table called "marital_statuses"...
Jim
A: 
<option value="single" <?php if($_REQUEST['maritalStatus'] == 'single'): ?>selected="selected"<?php endif ?>>Single</option>

And yes, you have to do an if for every option.

Mewp
+1  A: 

Or, to avoid adding an if to each option, just do something like:

echo '<select name="maritalStatus">';
$status = array("Select one", "Married", "Single", "Divorced");
foreach($status as $s)
      {
      $sel = ($maritalStatus == $s) ? 'selected = "selected"' : '';
      echo '<option value="'.$s.'" '.$sel.'>'.$s.'</option>';
      }
echo '</select>';

EDIT:

To dynamically populate the select from the DB you could:

echo '<select name="maritalStatus">';

$res = mysql_query("SELECT status FROM marital_status");
while ($row = mysql_fetch_array($res))
      {
      $s = $row['status']
      $sel = ($maritalStatus == $s) ? 'selected = "selected"' : '';
      echo '<option value="'.$s.'" '.$sel.'>'.$s.'</option>';
      }
echo '</select>';
nico
How could I have the "married", "single", "divorced" populate dynamically from the database? Suppose those 3 options weren't hard coded but existed in a separate table called "marital_statuses"...
Jim
@Jim: updated my answer
nico