upperbid Posted June 12, 2009 Share Posted June 12, 2009 Hi, I have a user update page in PHP where the user can update their personal information. At registration and the update page, I use a dropdown menu for the state like so: print '<td width="160"><p align="right">State</td>'; print '<td width="600"><p align="left"><select name="state">'; print '<option value="0" selected="selected">Select Your State</option>'; print '<option value="AL" selected_AL>Alabama</option>'; print '<option value="AS" selected_AS>American Samoa</option>'; print '<option value="AK" selected_AK>Alaska</option>'; I am able to return the data from the stored database fine, but the problem I am having is getting the dropdown menu to change to the retrieved state as it's selection. For example, Alabama would be stored in my database as AL. How do I make it so when AL is returned for the user from the stored info in the database, the dropdown menu is set for Alabama instead of "Select Your State"? The dropdown menu itself is in the HTML, the only thing being returned by the database is the state such as AL. I need to know how to code it so when the state is returned, I can set the HTML dropdown menu to show the proper state selected. Thanks for any help with this PHP problem. It is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/161919-solved-return-stored-state-in-dropdown-menu-on-user-update-page/ Share on other sites More sharing options...
harristweed Posted June 12, 2009 Share Posted June 12, 2009 It's not a PHP problem it's an HTML problem: $select = "selelected = \"selected\" "; print '<td width="160"><p align="right">State</td>'; print '<td width="600"><p align="left"><select name="state">'; print '<option value="0" '; if(empty($state)echo $select; print '>Select Your State</option>'; print '<option value="AL" '; if($state=="AL")echo $selec; print'>Alabama</option>'; print '<option value="AS" ; $state=="AL")echo $selec; >American Samoa</option>'; print '<option value="AK" '; $state=="AK")echo $selec; print'>Alaska</option>'; etc..... Quote Link to comment https://forums.phpfreaks.com/topic/161919-solved-return-stored-state-in-dropdown-menu-on-user-update-page/#findComment-854341 Share on other sites More sharing options...
harristweed Posted June 12, 2009 Share Posted June 12, 2009 not quite right but in a hurry to go out......one correction if(empty($state))echo $select; Quote Link to comment https://forums.phpfreaks.com/topic/161919-solved-return-stored-state-in-dropdown-menu-on-user-update-page/#findComment-854342 Share on other sites More sharing options...
thebadbad Posted June 12, 2009 Share Posted June 12, 2009 Have a look at this. Let's store the states in an array and output them in a loop, selecting the state that matches $state. That saves us from using a lot of repetitive code: <?php $state = 'AK'; //from database $states = array('AL' => 'Alabama', 'AS' => 'American Samoa', 'AK' => 'Alaska'); foreach ($states as $short => $long) { $selected = ($state == $short) ? ' selected="selected"' : ''; echo "<option value=\"$short\"$selected>$long</option>\n"; } ?> If you show me the full list of options (as you have them in your code), I'll create the $states array for you. Can be done quickly with regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/161919-solved-return-stored-state-in-dropdown-menu-on-user-update-page/#findComment-854368 Share on other sites More sharing options...
upperbid Posted June 13, 2009 Author Share Posted June 13, 2009 Thanks harristweed and thebadbad, I was able to use your info and now it works great. Quote Link to comment https://forums.phpfreaks.com/topic/161919-solved-return-stored-state-in-dropdown-menu-on-user-update-page/#findComment-854899 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.