My problem is when the entries in the form are validated and some fields with incorrect entries are found the fields filled out stay on the re-load of the form, all of them except the US States drop down menu which reverts back to Alabama every time.
I would like to see that the user selection stays in place even after a re-load of the form. and with my limited experience I am having an issue trying to figure this out for a school project.
Any help at all would be greatly appreciated, like what variables to create/call on and where exactly I insert this code.
Much appreciated.
here is the display code:
<?php
/* Program name: exercise_4_form.inc
* Description: Defines a form that collects a user's
* name and mailing address.
*/
$rows = array( "first_name" => "First Name (optional)",
"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: 15%;
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='$_SERVER[PHP_SELF]' method='POST'>";
foreach($rows as $field => $label)
if($field == "state")
{
echo "<label for='$field'>$label</label>
<select id='$field' name='$field'>\n";
foreach ( $states_list as $abbr => $sname )
{
echo "<option value='$abbr'>$sname</option>\n";
}
echo "</select>\n";
}
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='hidden' name='submitted' value='yes'>
<input type='submit' value='$submit'></div>";
?>
</form>
</body>
</html>Here is the Logic on the combined php file that displays and has the statements on it for the form:
<?php
/* Program name: exercise_4_form_verifications.php
* Description: Program checks all the form fields for
* blank fields and verifies them
*/
if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
{
foreach($_POST as $field => $value)
{
if(empty($value))
{
if($field != "first_name")
{
$blank_array[] = $field;
}
}
else
{
$good_data[$field] = strip_tags(trim($value));
}
}
if(@sizeof($blank_array) > 0)
{
$message = "<p style='color: black; margin-bottom: 0;font-weight: bold; margin-left: 35%'>
You didn't fill in one or more required fields.
You must enter:
<ul style='color: yellow; margin-top: 0; margin-left: 35%;
list-style: none' >";
foreach($blank_array as $value)
{
$message .= "<li>$value</li>";
}
$message .= "</ul>";
echo $message;
extract($good_data);
include("exercise_4_form.inc");
exit();
}
foreach($_POST as $field => $value)
{
if(!empty($value))
{
$name_verify = "/^[A-Za-z' -]{1,50}$/";
$phone_verify = "/^[0-9)(xX -]{7,20}$/";
$city_verify = "/^[A-Za-z' -]{1,50}$/";
$address_verify = "/^[A-Za-z0-9 .,'-]{1,50}$/";
$zip_verify = "/^[0-9]{5}(\-[0-9]{4})?$/";
$e_mail_verify = "/^.+@.+\\..+$/";
if(preg_match("/last/i",$field))
{
if(!preg_match($name_verify,$value))
{
$error_array[] = "$value is not a valid last name";
}
}
if(preg_match("/phone/i",$field))
{
if(!preg_match($phone_verify,$value))
{
$error_array[] = "$value is not a valid phone number";
}
} // endif phone format check
if(preg_match("/city/i",$field))
{
if(!preg_match($city_verify,$value))
{
$error_array[] = "$value is not a valid city";
}
}
if(preg_match("/address/i",$field))
{
if(!preg_match($address_verify,$value))
{
$error_array[] = "$value is not a valid address";
}
}
if(preg_match("/zip/i",$field))
{
if(!preg_match($zip_verify,$value))
{
$error_array[] = "$value is not a valid ZIP code";
}
}
if(preg_match("/e_mail/i",$field))
{
if(!preg_match($e_mail_verify,$value))
{
$error_array[] = "$value is not a valid e-mail address";
}
}
}
$clean_data[$field] = strip_tags(trim($value));
}
if(@sizeof($error_array) > 0)
{
$message = "<ul style='color: yellow; margin-top: 0; margin-left: 35%;
list-style: none' >";
foreach($error_array as $value)
{
$message .= "<li>$value</li>";
}
$message .= "</ul>";
echo $message;
extract($clean_data);
include("exercise_4_form.inc");
exit();
}
else
{
echo "<ol>";
foreach($_POST as $field => $value)
{ echo "<h3><li> $field = $value</li></h3>";
}
echo "</ol>";
}
}
else
{
include("exercise_4_form.inc");
}
?>