如何在表单(PHP / MySQL)中预先选择下拉选项和复选框选项?

时间:2010-06-20 13:55:01

标签: php html forms

我正在创建一个更新表单,其中抓取以前从mysql数据库提交的信息以填充当前表单。所以这就是我到目前为止所做的:

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

如您所见,我能够显示通过输入文本框或textarea输入的数据。但是如何让“婚姻状况”下拉预先选择“离婚”选项和“最喜欢的食物”下的“橘子”复选框GIVEN这两个选项是否实际存在于数据库中?

4 个答案:

答案 0 :(得分:2)

或者,为避免在每个选项中添加if,只需执行以下操作:

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

编辑:

要动态填充数据库中的选择,您可以:

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

答案 1 :(得分:0)

要预先选择in中的选项,您必须设置selected属性,如下所示:

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

<强>更新 或更多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; ?>

答案 2 :(得分:0)

<option value="single" <?php if($_REQUEST['maritalStatus'] == 'single'): ?>selected="selected"<?php endif ?>>Single</option>

是的,你必须为每个选项做一个if。

答案 3 :(得分:0)

复选框

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

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

选择

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

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

条件选择

使用jsp的示例,其中optionString是要比较的变量:

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