Jump to content

How can I populate a USA States select box with an array call?


anthonyJ

Recommended Posts

I am having a helluva time trying to populate my state option box with a state array.

I have the array set but now how does this get called into the foreach block where the select box is? I can populate it i guess with options manually but I want to get the array to display in the box instead.

I tried everything with the minimal experience I have so I need a little help, I got the select box up and displaying where I want it but I cant populate it with an array call.

 

See foreach block near the end of the code:

Thanks a bunch!

here is my include file code so far:

<?php
 /*  Program name: form_test_state.inc
  *  Description:  Defines a form that collects a user's
  *                name and mailing address.
  */
$rows = array( "first_name" => "First Name",
                 "last_name" => "Last Name",
                 "phone" => "Phone Number",
                 "city" => "City",
                 "state" => "State",
                 "address" => "Address",
                 "zip_code" => "ZIP Code",
                 "e_mail" => "E-Mail");
    
    $states_list = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa",  'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming");            
                
              
$submit = "Submit mailing information";
?>
<html>
<head>
  <style type='text/css'>
   <!--
body {
    background: #42413C;
    color: #000;
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 90%;
}
      form {
        margin: 1em 2 2 2;
        padding: 1;
        width: 1100px;
        margin-left: 35%;

      }
      .field {padding-bottom: 1em;
    
}
        label {
        font-weight: bold;
        float: left;
       width: 10%;
margin-right: 1em;
        text-align: right;
        background-color: #9F6;
padding-right: 5px;
        
      }
      select {margin-bottom: 1em;}
      
      #submit {
        margin-left: 15%;
      }
h3 {
width: 500px;
margin-left: 35%;
}


   -->
 </style>
</head>
<body>
<h3>Please enter your mailing information below:</h3>
<?php
 /* loop that displays the form */

  echo "<form action='checkBlankOnly_latest.php' method='post'>";

  foreach($rows as $field => $label)

  if($field == "state")
 {
      echo "<label for='$field'>$label</label>
      <select name='State'>                          <<<<  Insert $states_list array here I am trying to figure out >>>>
  <option value='state'>Alabama </option>
</select> ";

   }
   else  
  {
    echo "<div class='field'><label for='$field'>$label</label>
            <input id='$field' name='$field' type='text' value='".@$$field."'  
   size='25%' maxlength='65' /></div>\n";
  }
  echo "<div id='submit'>
    <input type='submit' value='$submit'></div>";
?>

</form>
</body>
</html>

Try this:

//...
foreach($states_list as $state)
{
     echo "<option value={$state}>{$state}</option>";
}
//...

 

Note that you'll need quotes around the value attribute's value.

echo "<option value='$state'>$state</option>";

Otherwise, states with spaces in the name (like "New York") won't be passed through the form submission correctly.

 

 

Side note: that <label> tag isn't really doing anything. To connect it with the corresponding form field, you could move the end </label> tag after the end </select> tag.

<?php
echo "<label>$label";
echo "<select name='State'>";
foreach($states_list as $state)
{
     echo "<option value='$state'>$state</option>";
}
echo "</select>";
echo '</label>';
?>
 
Note that I removed the "for" attribute from the <label> tag since it's no longer needed. More information about the form label, including an example of how to get the "for" attribute to work, can be found here:

Archived

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

×
×
  • 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.