Jump to content

Archived

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

steve m

Trouble displaying posted info in a drop down list

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

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
Thanks again printf.  That worked.  I kind of understand what you did.  That was nothing that I tought of.  Thanks again.

Share this post


Link to post
Share on other sites

×

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.