Reputation: 946
Let's say I have an HTML dropdown menu as,
<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>
By selecting one of the options, I am adding the particular value to the database as a VARCHAR.
Now I am calling them back, let's say, for an edit, so I need the entered data back to the same form, where I could retrieve all the other things to text boxes. But I have no idea how to get the value from the db and make it selected in this dropdown list. All I could do is saving the retrieved value from db to a variable named, $otherproblem
Upvotes: 1
Views: 905
Reputation: 1557
You have two options, one is to load the list along the lines of:
<?
$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>
<? } ?>
Dirty, but it will work with the way you have it setup currently. You can also rig it by simply making the item select at the top of the list, and then the full list like this:
<?
$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>
<? } ?>
There is much neater ways to do this, but without seeing how you are pulling the data, these two options will work in any case.
Upvotes: 1
Reputation: 1373
Here is a way to do this:
<?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>
Way 2:
<?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>
Upvotes: 1
Reputation: 51052
Often, you will be loading the list of options from the database as well. You'll loop through the list and construct the <option>
tags on the fly. As you do that, you compare the option value being created with the value retrieved from the earlier submission, and, if they match, you add the "selected" attribute to the option tag.
Upvotes: 1
Reputation: 5649
You would modify each option tag to look like this:
<option value="Nothing" <?=$otherproblem === 'Nothing' ? 'selected="selected"' : ''?>>Nothing</option>
Each one should compare the value from the database to the value of the option tag and add the selected attribute if they are equal.
Upvotes: 1