jeff5656 Posted April 6, 2008 Share Posted April 6, 2008 When updating a record, I want the form to be pre-populated, but I want the values in the drop down menu to also show what the record values are. the following does not work though. The drop down doesn't display the values, it displays "Please Select" even when there was an actual value for that record: <th>Sex</th><?php $select = isset($_POST["sex"]) ? $_POST["sex"] : ''; ?> <td><select name = "sex" > <option value="" <?php if ($select == '') echo 'selected="selected"'; ?>>Please Select</option> <option value="m" <?php if ($select == 'm') echo 'selected="selected"'; ?>>Male</option> <option value="f" <?php if ($select == 'f') echo 'selected="selected"'; ?>>Female</option> </select></td></tr> <tr></tr> <th>Race</th> <td><select name = "race" > <option value="" <?php if ($select == '') echo 'selected="selected"'; ?>>Please Select</option> <option value="b" <?php if ($select == 'b') echo 'selected="selected"'; ?>>Black</option> <option value="w" <?php if ($select == 'w') echo 'selected="selected"'; ?>>White</option> <option value="o" <?php if ($select == 'o') echo 'selected="selected"'; ?>>Other</option> </select></td></tr> Link to comment https://forums.phpfreaks.com/topic/99821-pre-populating-drop-down-menus-in-a-form-select/ Share on other sites More sharing options...
jeff5656 Posted April 6, 2008 Author Share Posted April 6, 2008 Anyone have any ideas? How do I get the value from the record to be the default value in the dropdown menu? Link to comment https://forums.phpfreaks.com/topic/99821-pre-populating-drop-down-menus-in-a-form-select/#findComment-510557 Share on other sites More sharing options...
maexus Posted April 6, 2008 Share Posted April 6, 2008 To set the default value of a drop down is HTML, not PHP My suggestion is to visit http://htmldog.com/ Link to comment https://forums.phpfreaks.com/topic/99821-pre-populating-drop-down-menus-in-a-form-select/#findComment-510559 Share on other sites More sharing options...
graham23s Posted April 6, 2008 Share Posted April 6, 2008 Hi Jeff, This is what i do: <?php // gender array // $gender_array = array("1"=>"Male","2"=>"Female"); $gender = $row['gender']; <-- 1 or 2 coming from mysql (depending on what the user has selected) // then // print("<select name='sex'>"); foreach($gender_array as $key => $gender_value) { print("<option value='$key' "); // if for selected // if($key == $gender) { print(" selected"); } print(">$gender_value</option>\n"); } print("</select>\n"); ?> hope that helps. Graham Link to comment https://forums.phpfreaks.com/topic/99821-pre-populating-drop-down-menus-in-a-form-select/#findComment-510585 Share on other sites More sharing options...
jeff5656 Posted April 6, 2008 Author Share Posted April 6, 2008 Hi, that doesn't work. If I have a female it still shows "male" as the default on the drop down. I changed "1" and "2" to "m" and "f", but otherwise kept everything else you had the same. Now if i select female and hit submit, it correctly changes the value (confirmed when I view it in phpmmyadmin), but when I go back to edit the record, the form always displays male as the default. Link to comment https://forums.phpfreaks.com/topic/99821-pre-populating-drop-down-menus-in-a-form-select/#findComment-510620 Share on other sites More sharing options...
graham23s Posted April 6, 2008 Share Posted April 6, 2008 Hi Mate, this is really the bit we are after: <?php // if for selected // if($key == $gender) { print(" selected"); } ?> $gender <-- this is basically the variable your grabbing from mysql in the gender table it will store either the 1 or 2 $key <-- when $gender matches one of the keys (1 or 2) it will echo the selected are you pulling the users gender from mysql at all? Graham Link to comment https://forums.phpfreaks.com/topic/99821-pre-populating-drop-down-menus-in-a-form-select/#findComment-510629 Share on other sites More sharing options...
jeff5656 Posted April 6, 2008 Author Share Posted April 6, 2008 I made a stupid mistake - my value is called "sex" not gender. Once I changed the $gender to $sex, it works. Thanks! Now if only someone could help me with my problem put forth in the javascript forum (subject "2 java scripts in one php file"). Link to comment https://forums.phpfreaks.com/topic/99821-pre-populating-drop-down-menus-in-a-form-select/#findComment-510654 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.