Jump to content

[SOLVED] return stored state in dropdown menu on user update page


upperbid

Recommended Posts

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.

Link to comment
Share on other sites

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.....

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.