Jump to content

Trouble displaying posted info in a drop down list


steve m

Recommended Posts

Hi,

I've built a dynamic drop down list to show US States in a form.  It was built using a function in a seperate file.  The file is included in the file I need it to be.  All of that works fine, but what I want to do is when the form is submitted and the validation comes back with input errors I want the the rop down list to display the state that the user submitted before.  I've tried everything I can think of but it doesn't work.  The drop down list keeps defaulting to the first item in the array that the list is built from.  Does anybody have any suggestion on what I can do?

Thanks
Link to comment
Share on other sites

here is the function from the seperate file.
[code=php:0]
function buildstateDD(){

$stateDD = array("None"=>"",
            "ALABAMA"=>"AL", "ALASKA"=>"AK", "ARIZONA"=>"AZ", "ARKANSAS"=>"AR",
"CALIFORNIA"=>"CA","COLORADO"=>"CO","CONNECTICUT"=>"CT","DELAWARE"=>"DE",
"DISTRICT OF COLUMBIA"=>"DC","FLORIDA"=>"FL","GEORGIA"=>"GA","HAWAII"=>"HI","IDAHO"=>"ID",
"ILLINOIS"=>"IL","INDIANA"=>"IN","IOWA"=>"IA","KANSAS"=>"KS","KENTUCKY"=>"KY",
"LOUISIANA"=>"LA","MAINE"=>"ME","MARYLAND"=>"MD","MASSACHUSETTS"=>"MA",
"MICHIGAN"=>"MI","MINNESOTA"=>"MN","MISSISSIPPI"=>"MS","MISSOURI"=>"MO",
"MONTANA"=>"MT","NEBRASKA"=>"NE","NEVADA"=>"NV","NEW HAMPSHIRE"=>"NH",
"NEW JERSEY"=>"NJ","NEW MEXICO"=>"NM","NEW YORK"=>"NY","NORTH CAROLINA"=>"NC",
"NORTH DAKOTA"=>"ND","OHIO"=>"OH","OKLAHOMA"=>"OK","OREGON"=>"OR",
"PENNSYLVANIA"=>"PA","RHODE ISLAND"=>"RI","SOUTH CAROLINA"=>"SC","SOUTH DAKOTA"=>"SD",
"TENNESSEE"=>"TN","TEXAS"=>"TX","UTAH"=>"UT","VERMONT"=>"VT","VIRGINIA"=>"VA",
"WASHINGTON"=>"WA","WEST VIRGINIA"=>"WV","WISCONSIN"=>"WI","WYOMING"=>"WY");

    foreach($stateDD as $State=>$sabrev)
    {
$disstate = ucwords(strtolower($State));
      $state_dd .= "<option value=\"$disstate\">$disstate</option>";
    }
  return $state_dd;
}
[/code]

here is the code fron the form.

<select class="formfield" size="1" name="state" value="<?php echo $state; ?>" tabindex="9">
<?php echo buildstateDD(); ?>
</select>&nbsp;

that is the code to build the drop down.  I tried an if statement so when the validation reports an error it will set a varible and I tried a condition so when that varible is set, I would try to echo that varible in the drop down.

I hope this code is readable.  I'm new to functions.
Link to comment
Share on other sites

You can't do that... (value='') is disallowed in the declaration! (remove it!)

[code]<select class="formfield" size="1" name="state" value="<?php echo $state; ?>" tabindex="9">[/code]

Here is the code to reselect the option if there is a error!

[code]function buildstateDD()
{
$stateDD = array("None"=>"",
            "ALABAMA"=>"AL", "ALASKA"=>"AK", "ARIZONA"=>"AZ", "ARKANSAS"=>"AR",
"CALIFORNIA"=>"CA","COLORADO"=>"CO","CONNECTICUT"=>"CT","DELAWARE"=>"DE",
"DISTRICT OF COLUMBIA"=>"DC","FLORIDA"=>"FL","GEORGIA"=>"GA","HAWAII"=>"HI","IDAHO"=>"ID",
"ILLINOIS"=>"IL","INDIANA"=>"IN","IOWA"=>"IA","KANSAS"=>"KS","KENTUCKY"=>"KY",
"LOUISIANA"=>"LA","MAINE"=>"ME","MARYLAND"=>"MD","MASSACHUSETTS"=>"MA",
"MICHIGAN"=>"MI","MINNESOTA"=>"MN","MISSISSIPPI"=>"MS","MISSOURI"=>"MO",
"MONTANA"=>"MT","NEBRASKA"=>"NE","NEVADA"=>"NV","NEW HAMPSHIRE"=>"NH",
"NEW JERSEY"=>"NJ","NEW MEXICO"=>"NM","NEW YORK"=>"NY","NORTH CAROLINA"=>"NC",
"NORTH DAKOTA"=>"ND","OHIO"=>"OH","OKLAHOMA"=>"OK","OREGON"=>"OR",
"PENNSYLVANIA"=>"PA","RHODE ISLAND"=>"RI","SOUTH CAROLINA"=>"SC","SOUTH DAKOTA"=>"SD",
"TENNESSEE"=>"TN","TEXAS"=>"TX","UTAH"=>"UT","VERMONT"=>"VT","VIRGINIA"=>"VA",
"WASHINGTON"=>"WA","WEST VIRGINIA"=>"WV","WISCONSIN"=>"WI","WYOMING"=>"WY");

foreach ( $stateDD as $State => $sabrev )
{
$disstate = ucwords ( strtolower ( $State ) );
$state_dd .= '<option value="' . $disstate . '"' . ( isset ( $_POST['state'] ) && $_POST['state'] == $disstate ? ' selected="selected"' : null ) . '>' . $disstate . '</option>';
}

return $state_dd;
}[/code]


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