Jump to content

States List with Selection


JasonHarper

Recommended Posts

Hello,

I need help with what I think is an easy issue.  I have a list of all states in a MySQL database.  I have a form where the user selects a state from a drop down list and I have that populated with info from the DB.  That part is working perfectly.  However, when the user returns to that page I'm pre-populating the form with their data.  What's happening is that the user's state appears correctly in the pre-filled drop down list, but it appears at the top and then again in the list from the DB.  Example:

 

If user's state is Arizona, the drop down list would look something like this:

 

Arizona

Alabama

Alaska

Arizona

...

 

My question is this....how do I make the user's state appear only once in the list and in the correct location (alphabetical order)?

 

Thank you!

Jason

Link to comment
https://forums.phpfreaks.com/topic/189767-states-list-with-selection/
Share on other sites

When you are outputting the the states from the database when creating the menu, check the user's state against each value. If you find a match output selected="selected" in the option tag. You don't need to add an additional option from the user's state.

 

eg.

<option value='Arizona' selected='selected'>Arizona</option>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.