steve m Posted October 7, 2006 Share Posted October 7, 2006 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 Quote Link to comment Share on other sites More sharing options...
printf Posted October 7, 2006 Share Posted October 7, 2006 Please show your code!me! Quote Link to comment Share on other sites More sharing options...
steve m Posted October 7, 2006 Author Share Posted October 7, 2006 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> 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. Quote Link to comment Share on other sites More sharing options...
printf Posted October 7, 2006 Share Posted October 7, 2006 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! Quote Link to comment Share on other sites More sharing options...
steve m Posted October 7, 2006 Author Share Posted October 7, 2006 Thanks again printf. That worked. I kind of understand what you did. That was nothing that I tought of. Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.