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.

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

 

 

 

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.

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.