1

假设我有一个 HTML 下拉菜单,

<p>General Medical History: 
<select name="otherproblem">
    <option value="Nothing">Nothing</option>
    <option value="Allergy: Penicillin">Allergy: Penicillin</option>
    <option value="Aspirin">Aspirin</option>
    <option value="Erythromycin">Erythromycin</option>
    <option value="Latex or Rubber Products">Latex or Rubber Products</option>
    <option value="Codeine">Codeine</option>
    <option value="Tetracycline">Tetracycline</option>
    <option value="Germicides/Pesticides, Foods">Germicides/Pesticides, Foods</option>
    <option value="Other">Other</option>
    <option value="Asthma">Asthma</option>
    <option value="Bleeding Disorders">Bleeding Disorders</option>
    <option value="Diabetes">Diabetes</option>
    <option value="Epilepsy">Epilepsy</option>
    <option value="GI disorders">GI disorders</option>
    <option value="Heart disease">Heart disease</option>
    <option value="Hepatitis">Hepatitis</option>
    <option value="Jaundice">Jaundice</option>
    <option value="Liver disease">Liver disease</option>
    <option value="Neoplasm">Neoplasm</option>
    <option value="Psychiatric Problems">Psychiatric Problems</option>
    <option value="Respiratory diseases">Respiratory diseases</option>
    <option value="Rheumatic fever">Rheumatic fever</option>
</select>
</p>

通过选择其中一个选项,我将特定值作为 VARCHAR 添加到数据库中。

现在我要给他们回电话,比如说进行编辑,所以我需要将输入的数据返回到同一个表单,在那里我可以将所有其他内容检索到文本框中。但我不知道如何从数据库中获取值并在此下拉列表中选择它。我所能做的就是将检索到的值从 db 保存到一个名为的变量中, $otherproblem

4

4 回答 4

1

您可以将每个选项标签修改为如下所示:

<option value="Nothing" <?=$otherproblem === 'Nothing' ? 'selected="selected"' : ''?>>Nothing</option>

每个人都应该将数据库中的值与选项标签的值进行比较,如果它们相等,则添加选定的属性。

于 2013-01-13T23:51:34.863 回答
1

通常,您也会从数据库中加载选项列表。您将遍历列表并动态构建<option>标签。当您这样做时,您将创建的选项值与从先前提交中检索到的值进行比较,如果它们匹配,则将“selected”属性添加到选项标记中。

于 2013-01-13T23:52:51.560 回答
1

这是一种方法:

<?php

$otherproblem = "Aspirin";

$dropdown = array(
    "Nothing",
    "Allergy: Penicillin",
    "Aspirin",
    "Erythromycin",
    "Latex or Rubber Products",
    "Codeine",
    "Tetracycline",
    "Germicides/Pesticides, Foods",
    "Other",
    "Asthma",
    "Bleeding Disorders",
    "Diabetes",
    "Epilepsy",
    "GI disorders",
    "Heart disease",
    "Hepatitis",
    "Jaundice",
    "Liver disease",
    "Neoplasm",
    "Psychiatric Problems",
    "Respiratory diseases",
    "Rheumatic fever"
);

?>
<p>General Medical History: 
<select name="otherproblem">
    <?php foreach ($dropdown as $name): ?>
        <option <?php if ($otherproblem == $name) print('selected="selected"');?> value="<?php print($name);?>"><?php print($name);?></option>
    <?php endforeach; ?>
</select>
</p>

方式二:

<?php
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
    <option <?php if($otherproblem == "Nothing"){print('selected="selected"');}?> value="Nothing">Nothing</option>
    <option <?php if($otherproblem == "Allergy: Penicillin"){print('selected="selected"');}?> value="Allergy: Penicillin">Allergy: Penicillin</option>
    <option <?php if($otherproblem == "Aspirin"){print('selected="selected"');}?> value="Aspirin">Aspirin</option>
    <option <?php if($otherproblem == "Erythromycin"){print('selected="selected"');}?> value="Erythromycin">Erythromycin</option>
    <option <?php if($otherproblem == "Latex or Rubber Products"){print('selected="selected"');}?> value="Latex or Rubber Products">Latex or Rubber Products</option>
    <option <?php if($otherproblem == "Codeine"){print('selected="selected"');}?> value="Codeine">Codeine</option>
    <option <?php if($otherproblem == "Tetracycline"){print('selected="selected"');}?> value="Tetracycline">Tetracycline</option>
    <option <?php if($otherproblem == "Germicides/Pesticides, Foods"){print('selected="selected"');}?> value="Germicides/Pesticides, Foods">Germicides/Pesticides, Foods</option>
    <option <?php if($otherproblem == "Other"){print('selected="selected"');}?> value="Other">Other</option>
    <option <?php if($otherproblem == "Asthma"){print('selected="selected"');}?> value="Asthma">Asthma</option>
    <option <?php if($otherproblem == "Bleeding Disorders"){print('selected="selected"');}?> value="Bleeding Disorders">Bleeding Disorders</option>
    <option <?php if($otherproblem == "Diabetes"){print('selected="selected"');}?> value="Diabetes">Diabetes</option>
    <option <?php if($otherproblem == "Epilepsy"){print('selected="selected"');}?> value="Epilepsy">Epilepsy</option>
    <option <?php if($otherproblem == "GI disorders"){print('selected="selected"');}?> value="GI disorders">GI disorders</option>
    <option <?php if($otherproblem == "Heart disease"){print('selected="selected"');}?> value="Heart disease">Heart disease</option>
    <option <?php if($otherproblem == "Hepatitis"){print('selected="selected"');}?> value="Hepatitis">Hepatitis</option>
    <option <?php if($otherproblem == "Jaundice"){print('selected="selected"');}?> value="Jaundice">Jaundice</option>
    <option <?php if($otherproblem == "Liver disease"){print('selected="selected"');}?> value="Liver disease">Liver disease</option>
    <option <?php if($otherproblem == "Neoplasm"){print('selected="selected"');}?> value="Neoplasm">Neoplasm</option>
    <option <?php if($otherproblem == "Psychiatric Problems"){print('selected="selected"');}?> value="Psychiatric Problems">Psychiatric Problems</option>
    <option <?php if($otherproblem == "Respiratory diseases"){print('selected="selected"');}?> value="Respiratory diseases">Respiratory diseases</option>
    <option <?php if($otherproblem == "Rheumatic fever"){print('selected="selected"');}?> value="Rheumatic fever">Rheumatic fever</option>
</select>
</p>
于 2013-01-13T23:54:47.690 回答
1

您有两种选择,一种是按照以下方式加载列表:

<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
if(strcmp($row['Problem'],$otherproblem)===0){ $selected = 'selected="selected"'; } else { $selected = ""; }
?>
<option <?=$other?> value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>

很脏,但它可以按照您当前的设置方式工作。您也可以通过简单地在列表顶部选择项目,然后像这样的完整列表来装配它:

<?
    $otherproblem = "Erythromycin";
?>
<p>General Medical History: 
<select name="otherproblem">
<option selected="selected" value="<?=$otherproblem;?>"><?=$otherproblem;?></option>
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
?>
<option value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>

有很多更简洁的方法可以做到这一点,但是在没有看到你如何提取数据的情况下,这两个选项无论如何都可以工作。

于 2013-01-14T00:08:16.013 回答