开发者

How to preselect drop down choices and checkbox options in a form (PHP/MySQL)?

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'];
    $maritalSta开发者_StackOverflowtus = $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?


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>';


<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.


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; ?>


Check Boxes

<input type="checkbox" id="id" name"checkbox" value="Option 1" checked>

or

<input type="checkbox" id="id" name"checkbox" value="Option 1" checked="checked">

Selection

<option value="Option 1"  selected>Option 1</option>

or

<option value="Option 1" selected="selected">Option 1</option>

Conditional Selection

An example using jsp, where optionString is the variable to compare:

<input type="checkbox" id="id" name"checkbox" value="Option 1" 
<%=(optionString.equalsIgnoreCase("Option 1"))? "checked":""%> />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜