adding drop down menu to user-signup form

2 posts by 2 authors in: Forums > CMS Builder
Last Post: March 13, 2013   (RSS)

By gregThomas - March 13, 2013

Hi Jeff,

You could try something like this:

<?php 
$stateArray = array(
  'ALABAMA'        => 'AL',
  'ALASKA'         => 'AK',
  'ARIZONA'        => 'AZ',
  'ARKANSAS'       => 'AR',
  'CALIFORNIA'     => 'CA',
  'COLORADO'       => 'CO',
  'CONNECTICUT'    => 'CT',
  'DELAWARE'       => 'DE',
  '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',
);

?>
<select name="state">
  <option value="">Select your state</option>
  <?php foreach($stateArray as $state => $code): ?>
    <option <?php selectedIf(@$_REQUEST['state'],$code); ?> value="<?php echo $code; ?>"><?php echo $state; ?></option>
  <?php endforeach; ?>
</select>


This is just example code, so you might have to change it to work with your page.

I created the array of states by copying the text from this page http://www.softschools.com/social_studies/state_abbreviations/, and formatted it using my text editor.

Then the foreach loop cycles through each state and adds an option to the select menu. The selectedIf function will add the text selected="selected" if the form has been submitted and that is the option they selected.

Let me know if you have any questions.

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com