melting_dog Posted July 7, 2010 Share Posted July 7, 2010 Hi all, I have a form that i can fill with php variables i.e: echo '<td><input name="pCode" type="text" value="' . $row['pCode'] . '" size="40" maxlength="120" /></td>'; But does anyone know how to preset a drop down list so the initial value is the php $row variable? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/206967-pass-php-variable-into-form-drop-down-list/ Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 Sure, however, the solution will be slightly different based upon where the total options come from. They should either be coming from a database or a hard-coded array. Here is one possible solution $colors = array('Red', 'Blue', 'Green', 'Yellow', 'Black', 'White'); $colorOptions = ''; foreach($colors as $color) { $selected = ($row['color']==$color) ? ' selected="selected"': ''; $colorOptions .= "<option value=\"{$color}\"{$selected}>{$color}</option>\n"; } echo "<select name=\"\">\n"; echo $colorOptions; echo "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/206967-pass-php-variable-into-form-drop-down-list/#findComment-1082283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.