Randall-B
asked on
Insert 'selected="selected" ' into HTML of Option List in Page Populated by MySQL, to Indicate Which Option Was Previously Selected, According to the Value Currently Stored in MySQL
In an edit/update form to update an existing record in MySQL, the form inputs are populated from the database. How do I code it to show which option was already selected in a dropdown option box?
For example, if the select box html is:
<select name="Comedian">
<option value="">Select Comedian</option>
<option value="Curly">Curly</optio n>
<option value="Larry">Larry</optio n>
<option value="Moe">Moe</option>
</select>
and the the $Comedian variable from MySQL = "Larry", how do I make the list echo like:
<option value="">Select Comedian</option>
<option value="Curly">Curly</optio n>
<option selected="selected" value="Larry">Larry</optio n>
<option value="Moe">Moe</option>
where Larry's row contains selected="selected" ?
Note: I'm not currently populating the entire list of options from MySQL; they're hard-coded into the html form and will not need to change. The list is currently in a plain-HTML portion of the page, and any php echos in that part are currently like <?php echo $var; ?> . Is there a way to make php insert the 'selected="selected" ' in the right row of the html list without having to populate the entire list from MySQL?
For example, if the select box html is:
<select name="Comedian">
<option value="">Select Comedian</option>
<option value="Curly">Curly</optio
<option value="Larry">Larry</optio
<option value="Moe">Moe</option>
</select>
and the the $Comedian variable from MySQL = "Larry", how do I make the list echo like:
<option value="">Select Comedian</option>
<option value="Curly">Curly</optio
<option selected="selected" value="Larry">Larry</optio
<option value="Moe">Moe</option>
where Larry's row contains selected="selected" ?
Note: I'm not currently populating the entire list of options from MySQL; they're hard-coded into the html form and will not need to change. The list is currently in a plain-HTML portion of the page, and any php echos in that part are currently like <?php echo $var; ?> . Is there a way to make php insert the 'selected="selected" ' in the right row of the html list without having to populate the entire list from MySQL?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great. Your first response is exactly what I was looking for, and I may look into using the format from your second comment. Thanks.
<?php
function contactEntity($entityName,
global $contactOptions, $userRecipient;
$contactOptions .= '<option value="' . $entityName . '" ' .
(($userRecipient==$entityN
}
# start list of possible recipients
contactEntity('person1','F
contactEntity('person2','F
contactEntity('person3','F
echo $contactOptions;
?>
where $userRecipient holds the value of the selected option, and the string of options is built by multiple calls to the function