gpt4 book ai didi

php - 使用 MySQL 数据库中的值填充 HTML 下拉列表并使其成为 'selected',而列表中还有其他选项

转载 作者:行者123 更新时间:2023-11-29 04:48:54 25 4
gpt4 key购买 nike

假设我有一个 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

的变量中

最佳答案

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

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

有很多更简洁的方法可以做到这一点,但在不了解您如何提取数据的情况下,这两个选项在任何情况下都可以使用。

关于php - 使用 MySQL 数据库中的值填充 HTML 下拉列表并使其成为 'selected',而列表中还有其他选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14309614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com