title: PHP drop down list using array's and foreach (else and for) code
body: <p>I'm taking a course called &quot;Dynamic Web Development with PHP&quot; and after getting an F for an exercise, I wish if you could help me with the following.</p>
  <p>I have a php file and an inc file (that shows the form).</p>
  <p>What I can't figure out how to resolve is the drop-down lists of the country and state / province, which should work like this:</p>
  <p>If country USA is selected populate US states.
  If country CAN is selected populate Canadian provinces.
  I've made to array's one is states_list (USA) and province_list (CAN)</p>
  <p>My teacher asking me to use the if statement to show the drop-down field.
  Exemple:</p>
  <pre><code>foreach($labels as $field =&gt; $label)
  {
    if($field == &quot;state&quot;)
    {
        echo html code for the drop-down list
     }
     else
     {
        echo html code for a text field
     }
  }
  </code></pre>
  <p>My inc file:</p>
  <pre><code>&lt;?php
   /*  Program name: form_data.inc
    *  Description:  Defines the form that collects a user's 
    *                personal information.
    */
  $labels = array( &quot;firstName&quot; =&gt; &quot;First Name&quot;,
                   &quot;lastName&quot; =&gt; &quot;Last Name&quot;,
                   &quot;street&quot; =&gt; &quot;Street&quot;,
                   &quot;city&quot; =&gt; &quot;City&quot;,
                   &quot;zip&quot; =&gt; &quot;Postal Code&quot;,
                   &quot;country&quot; =&gt; &quot;Country&quot;,
                   &quot;state&quot; =&gt; &quot;State&quot;,
                   &quot;email&quot; =&gt; &quot;E-mail&quot;,
                   &quot;phone&quot; =&gt; &quot;Phone&quot;,);
                   
  $country_list = array(&quot;USA&quot; =&gt; &quot;United States&quot;,  
                        &quot;CAN&quot; =&gt; &quot;Canada&quot;,);
                        
  $state_list = array( &quot;AL&quot;=&gt;&quot;AL&quot;,  
                       &quot;AK&quot;=&gt;&quot;AK&quot;,  
                       &quot;AZ&quot;=&gt;&quot;AZ&quot;,  
                       &quot;AR&quot;=&gt;&quot;AR&quot;,  
                       &quot;CA&quot;=&gt;&quot;CA&quot;,  
                       &quot;CO&quot;=&gt;&quot;CO&quot;,  
                       &quot;CT&quot;=&gt;&quot;CT&quot;,  
                       &quot;DE&quot;=&gt;&quot;DE&quot;,  
                       &quot;DC&quot;=&gt;&quot;DC&quot;,  
                       &quot;FL&quot;=&gt;&quot;FL&quot;,  
                       &quot;GA&quot;=&gt;&quot;GA&quot;,  
                       &quot;HI&quot;=&gt;&quot;HI&quot;,  
                       &quot;ID&quot;=&gt;&quot;ID&quot;,  
                       &quot;IL&quot;=&gt;&quot;IL&quot;,  
                       &quot;IN&quot;=&gt;&quot;IN&quot;,  
                       &quot;IA&quot;=&gt;&quot;IA&quot;,  
                       &quot;KS&quot;=&gt;&quot;KS&quot;,  
                       &quot;KY&quot;=&gt;&quot;KY&quot;,  
                       &quot;LA&quot;=&gt;&quot;LA&quot;,  
                       &quot;ME&quot;=&gt;&quot;ME&quot;,  
                       &quot;MD&quot;=&gt;&quot;MD&quot;,  
                       &quot;MA&quot;=&gt;&quot;MA&quot;,  
                       &quot;MI&quot;=&gt;&quot;MI&quot;,  
                       &quot;MN&quot;=&gt;&quot;MN&quot;,  
                       &quot;MS&quot;=&gt;&quot;MS&quot;,  
                       &quot;MO&quot;=&gt;&quot;MO&quot;,  
                       &quot;MT&quot;=&gt;&quot;MT&quot;,
                       &quot;NE&quot;=&gt;&quot;NE&quot;,
                       &quot;NV&quot;=&gt;&quot;NV&quot;,
                       &quot;NH&quot;=&gt;&quot;NH&quot;,
                       &quot;NJ&quot;=&gt;&quot;NJ&quot;,
                       &quot;NM&quot;=&gt;&quot;NM&quot;,
                       &quot;NY&quot;=&gt;&quot;NY&quot;,
                       &quot;NC&quot;=&gt;&quot;NC&quot;,
                       &quot;ND&quot;=&gt;&quot;ND&quot;,
                       &quot;OH&quot;=&gt;&quot;OH&quot;,
                       &quot;OK&quot;=&gt;&quot;OK&quot;,
                       &quot;OR&quot;=&gt;&quot;OR&quot;,
                       &quot;PA&quot;=&gt;&quot;PA&quot;,
                       &quot;RI&quot;=&gt;&quot;RI&quot;,
                       &quot;SC&quot;=&gt;&quot;SC&quot;,
                       &quot;SD&quot;=&gt;&quot;SD&quot;,
                       &quot;TN&quot;=&gt;&quot;TN&quot;,
                       &quot;TX&quot;=&gt;&quot;TX&quot;,
                       &quot;UT&quot;=&gt;&quot;UT&quot;,
                       &quot;VT&quot;=&gt;&quot;VT&quot;,
                       &quot;VA&quot;=&gt;&quot;VA&quot;,
                       &quot;WA&quot;=&gt;&quot;WA&quot;,
                       &quot;WV&quot;=&gt;&quot;WV&quot;,
                       &quot;WI&quot;=&gt;&quot;WI&quot;,
                       &quot;WY&quot;=&gt;&quot;WY&quot;);
  
  $province_list = array( &quot;AB&quot;=&gt;&quot;AB&quot;,
                          &quot;BC&quot;=&gt;&quot;BC&quot;,
                          &quot;MB&quot;=&gt;&quot;MB&quot;,
                          &quot;NB&quot;=&gt;&quot;NB&quot;,
                          &quot;NL&quot;=&gt;&quot;NL&quot;,
                          &quot;NS&quot;=&gt;&quot;NS&quot;,
                          &quot;NT&quot;=&gt;&quot;NT&quot;,
                          &quot;NU&quot;=&gt;&quot;NU&quot;,
                          &quot;ON&quot;=&gt;&quot;ON&quot;,
                          &quot;PE&quot;=&gt;&quot;PE&quot;,
                          &quot;QC&quot;=&gt;&quot;QC&quot;,
                          &quot;SK&quot;=&gt;&quot;SK&quot;,
                          &quot;YT&quot;=&gt;&quot;YT&quot;);
                      
  $radios = array( &quot;New&quot;, &quot;Changed&quot;);
  $submit = &quot;Submit Data&quot;;
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Form&lt;/title&gt;
    &lt;style type='text/css'&gt;
     &lt;!--
        body {
          background: #242523;
          font-family: Tahoma, Geneva, sans-serif;
        }
        form {
          width: 500px;
          margin: auto;
          padding: 10px 0 25px 0;
          -webkit-border-radius: 7px/9px; 
          -moz-border-radius: 7px/9px; 
          border-radius: 7px/9px; 
          background-color: #fff;
        }
        .field {padding-bottom: 1em;
           font-weight: normal;
           font-size:14px;
        }
        label {
          font-weight: normal;
          font-size:14px;
          float: left;
          width: 20%;
          margin-right: 1em;
          text-align: right;
        }
        input {
          
        }
        h3 {
            color:#C00;
            text-align: center;
            font-size:20px;
            padding-bottom: 10px;
        }
        #submit {
          margin-left: 22%;
        }
     --&gt;
   &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;br&gt;
  &lt;?php
   /* loop that displays the form */
    echo &quot;&lt;form action='$_SERVER[PHP_SELF]' method='POST'&gt;&quot;;
    echo &quot;&lt;h3&gt;Personal information&lt;/h3&gt;&quot;;
    
    foreach($labels as $field =&gt; $label)
    {
      echo &quot;&lt;div class='field'&gt;&lt;label for='$field'&gt;$label&lt;/label&gt;
            &lt;input id='$field' name='$field' type='text' value='&quot;.@$$field.&quot;'
               size='50%' maxlength='50' /&gt;&lt;/div&gt;\n&quot;;
    }
    
    echo &quot;&lt;div class='field'&gt;
            &lt;input type='radio' name='status' checked='checked'
              value='new' style='margin-left: 25%'/&gt;$radios[0]
            &lt;input type='radio' name='status' 
              value='changed' style='margin-left: 25%' /&gt;$radios[1]&lt;/div&gt;\n&quot;;
              
     echo &quot;&lt;div class='field'&gt;
      &lt;input type='hidden' name='submitted' value='yes' /&gt;&lt;/div&gt;\n&quot;;
     echo &quot;&lt;div id='submit'&gt;
      &lt;input type='submit' name='submitButton' value='$submit'&gt;&lt;/div&gt;&quot;;
      
  ?&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>My PHP file:</p>
  <pre><code>&lt;?php
  /*  Program name: checkBlank.php 
   *  Description:  Program displays the blank form and checks 
   *  all the form fields for blank fields.
   */
  if(isset($_POST['submitted']) and $_POST['submitted'] == &quot;yes&quot;)
  {
    foreach($_POST as $field =&gt; $value)           
    {
      if(empty($value))
      {
        if($field != &quot;firstName&quot;)
        {
           $blank_array[] = $field;
        }
      } 
      else                                                    
      {
        $good_data[$field] = strip_tags(trim($value));
      }
      
    }
    
    if(@sizeof($blank_array) &gt; 0) 
    {
    $message = &quot;&lt;p style='color: white; margin-bottom: 0; 
                   font-weight: bold'&gt;
                   You didn't fill in one or more required fields. 
                   You must enter: 
                   &lt;ul style='color: red; margin-top: 0; 
                   list-style: none' &gt;&quot;;
   /* display list of missing information */
      foreach($blank_array as $value)
      {
         $message .= &quot;&lt;li&gt;$value&lt;/li&gt;&quot;;
      }
      $message .= &quot;&lt;/ul&gt;&quot;; 
      echo $message;
      extract($good_data);
      include(&quot;form_data.inc&quot;);   
      exit();    
    }  // endif missing information
     /* format check */
      foreach($_POST as $field =&gt; $value)
    {
    if(!empty($value))
    {
      $name_patt = &quot;/^[A-Za-z' -]{1,50}$/&quot;;
      $addr_patt = &quot;/^[A-Za-z0-9 .,'-]{1,50}$/&quot;;
      $city_patt = &quot;/^[A-Za-z' -]{1,50}$/&quot;;
      $postal_patt = &quot;/^[A-Za-z0-9-]{1,10}$/&quot;;
      $state_patt = &quot;/^[A-Za-z]{2}$/&quot;;
      $province_patt = &quot;/^[A-Za-z]{2}$/&quot;;
      $country_patt = &quot;/^[A-Za-z' -]{1,50}$/&quot;;
      $email_patt = &quot;/^.+@.+\\..+$/&quot;;
      $phone_patt = &quot;/^[0-9)(xX -]{7,20}$/&quot;;
      $radio_patt = &quot;/(new|changed)/&quot;;
      /* Checks first and last names */
      if(preg_match(&quot;/name/i&quot;,$field))
      {
        if(!preg_match($name_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid name&quot;;
        }
      } 
      /* Checks address */
      if(preg_match(&quot;/street/i&quot;,$field))
      {
        if(!preg_match($addr_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid address&quot;;
        }
      } 
      /* Checks city */
      if(preg_match(&quot;/city/i&quot;,$field))
      {
        if(!preg_match($city_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid city name&quot;;
        }
      }   
      /* Checks postal code */
      if(preg_match(&quot;/zip/i&quot;,$field))
      {
        if(!preg_match($postal_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid potal code&quot;;
        }
      } 
      /* Checks country names*/ 
          if(preg_match(&quot;/country/i&quot;,$field))
      {
        if(!preg_match($country_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid country&quot;;
        }
      } 
      /* Checks state and province names */
      if(preg_match(&quot;/state/i&quot;,$field))
      {
        if(!preg_match($state_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid state&quot;;
        }
      }
      if(preg_match(&quot;/province/i&quot;,$field))
      {
        if(!preg_match($province_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid province&quot;;
        }
      }
      /* Checks email address */
      if(preg_match(&quot;/email/i&quot;,$field))
      {
        if(!preg_match($email_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid e-mail address&quot;;
        }
      } 
      /* Checks phone number */
      if(preg_match(&quot;/phone/i&quot;,$field))
      {
        if(!preg_match($phone_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid phone number&quot;;
        }
      }  // endif formats check
      if(preg_match(&quot;/status/i&quot;,$field))
      {
        if(!preg_match($radio_patt,$value))
        {
          $error_array[] = &quot;$value is not a valid status&quot;;
        }
      }
    } 
    $clean_data[$field] = strip_tags(trim($value));
  }
  if(@sizeof($error_array) &gt; 0) 
  {
  
    $message = &quot;&lt;ul style='color: red; list-style: none' &gt;&quot;;
    foreach($error_array as $value)
    {
     $message .= &quot;&lt;li&gt;$value&lt;/li&gt;&quot;;
    }
    $message .= &quot;&lt;/ul&gt;&quot;; 
  
     
    echo $message;
    extract($clean_data);
    include(&quot;form_data.inc&quot;);
    exit();   
  }
  else
  {
   echo &quot;Data is all okay&quot;;
  }
   /* displays all the information passed from a form */ 
      echo &quot;&lt;ol&gt;&quot;;
          foreach($_POST as $field =&gt; $value)
          {
               echo &quot;&lt;li&gt; $field = $value&lt;/li&gt;&quot;;
          }
      echo &quot;&lt;/ol&gt;&quot;;
  }
  else
  {
    include(&quot;form_data.inc&quot;);
  }   
  ?&gt;
  </code></pre>