I am very new to PHP and am having issues populating a drop down menu of states from an array. I need the choice to remain in the drop down in case the user submits with an error (i.e. an empty field or incorrectly formatted field). Here is my code. Any help is appreciated.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
[php]<?php
/* Program name: customer_form.inc
- Description: Defines a from that collects customerinformation.
*/
$labels = array ( “first_name” => “First Name”,
“middle_name” => “Middle Name”,
“last_name” => “Last Name”,
“address” => “Address”,
“city” => “City”,
“state” => “State”,
“zip” => “Zip”,
“email” => “Email”,
“phone” => “Phone”);
$states = array ( “AL” => “Alabama”,
“AK” => “Alaska”,
“AZ” => “Arizona”,
“AR” => “Arkansas”,
“CA” => “California”,
“CO” => “Colorado”,
“CT” => “Connecticut”,
“DE” => “Delaware”,
“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 Information”;
?>
[/php]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Customer Form</title>
<style type='text/css'>
<!--
form {
margin: 1.5em 0 0 0;
padding: 0;
}
.dropdown {
margin-left: 18%;
margin-top: 1em;
margin-bottom: 1.5em;
}
.field {padding-bottom: 1em;}
label {
font-weight: bold;
float: left;
width: 20%;
margin-right: 1em;
text-align: right;
}
#submit {
margin-left: 35%;
}
-->
</style>
</head>
<body>
<h3>Please enter your information below:</h3>
[php]<?php
/* loop that displays the form */
echo “”;
foreach($labels as $field=> $label)
{
if($field == “state”)
{
echo “
“”;
foreach($states as $state) {
echo “<option value=’”.@$$field."’)’.$state’";
}
echo “
else
{
echo"
<input id=’$field’ name=’$field’ type=‘text’ value=’".@$$field."’
size='50% maxlength=‘65’ />
}
}
echo "
?>
[/php]
[code]
[/code]