Jump to content


Photo

Trouble displaying posted info in a drop down list


  • Please log in to reply
4 replies to this topic

#1 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 07 October 2006 - 12:05 AM

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

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 07 October 2006 - 12:16 AM

Please show your code!

me!

#3 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 07 October 2006 - 12:28 AM

here is the function from the seperate file.
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;
	}

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.

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 07 October 2006 - 12:42 AM

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

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

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

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;
}


me!

#5 steve m

steve m
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 07 October 2006 - 12:52 AM

Thanks again printf.  That worked.  I kind of understand what you did.  That was nothing that I tought of.  Thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users