Redisplaying a drop-down menu choice...

jbdesign

New Member
Messages
26
Reaction score
0
Points
0
Ok here is the code of a test I am working on and for some reason I can't get it to redisplay the drop-down menu choice (which is a state abbreviation) after it encounters an error. It always goes back to the default " " as the choice.

Like I've said before I am fairly new to the game so I'm sure I haven't included a correct if statement or something really stupidly simple.

Any and all help would be greatly appreciated.
Code:
<?php 
/*  Program name: test_form.php 
 *  Description:  Displays a form that checks
 *  all fields to see if they are blank
 *  and checks them for possible miss-entered
 *  or incorrect information
 */ 
ini_set("display_errors","on"); 
error_reporting(E_ALL | E_STRICT); 
ini_set("include_path","./includes");
include("reginfo.inc");
if(isset($_POST['submitted']) and $_POST['submitted'] == "yes") 
{ 
  foreach($_POST as $field => $value)         
  { 
    if(empty($value)) 
    { 
	  if($field != "address2") 
      { 
         $blank_array[] = $field; 
      } 
    }
    else                                                 
    { 
      $good_data[$field] = strip_tags(trim($value)); 
    } 
  }
  if(@sizeof($blank_array) > 0) 
  {
  /*Display error message if information is not entered*/ 
    $message = "<p style='color: red; margin-bottom: 0; 
                 font-weight: bold'> 
                 You didn't fill in one or more required fields. 
                 You must enter: 
                 <ul style='color: red; margin-top: 0; 
                 list-style: none' >";
    foreach($blank_array as $value) 
    { 
       $message .= "<li>$value</li>"; 
    } 
    $message .= "</ul>"; 
    echo $message; 
    extract($good_data); 
    include("register_form5.inc"); 
    exit();    
  } 
  foreach($_POST as $field => $value) 
{ 
  if(!empty($value)) 
  { 
        $user_patt = "/^[A-Za-z0-9_]{5,20}$/";
	$pass_patt = "/(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{4,8})$/";
        $name_patt = "/^[A-Za-z' -]{1,50}$/";
	$address_patt = "/[0-9A-Za-z'. -]{5,50}$/";
	$city_patt = "/^[A-Za-z' -]{1,50}$/";
	$state_patt = "/(?i)^(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[ANU]
|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]
|UT|V[AIT]|W[AIVY])$/i";
	$zip_patt = "/^([0-9]{5})(-[0-9]{4})?$/i";
	$phone_patt = "/^[0-9)(xX -]{7,20}$/";
	$email_patt = "/^[+_a-z0-9-]+(\.[+_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i";
	$age_patt = "/^[0-9]{1}+[0-9]{1}$/"; 
	if(preg_match("/user/i",$field)) 
    { 
      if(!preg_match($user_patt,$value)) 
      { 
        $error_array[] = "$value is not a valid name"; 
      } //end of username check
	}
	if(!preg_match("/pass/i",$field)) 
    { 
      if(preg_match($pass_patt,$value)) 
      { 
        $error_array[] = "Please enter a password that is between 4 to 8 characters and contains at least an letter and number"; 
      } //end of password check
	}
    if(preg_match("/name/i",$field)) 
    { 
      if(!preg_match($name_patt,$value)) 
      { 
        $error_array[] = "$value is not a valid name"; 
      } //end of name check
    } 
    if(preg_match("/address/i",$field)) 
    { 
      if(!preg_match($address_patt,$value)) 
      { 
        $error_array[] = "$value is not a valid address"; 
      } //end of address check
	}
	if(preg_match("/city/i",$field))
	{ 
      if(!preg_match($city_patt,$value)) 
      { 
	   $error_array[] = "$value is not a valid City";
       } //end of city check
	}
	if(preg_match("/state/i",$field))
	{ 
      if(!preg_match($state_patt,$value)) 
      { 
        $error_array[] = "Please enter your state's two letter abbreviation, i.e. NY"; 
      } //end of state check
    }
	if(preg_match("/zip/i",$field))
	{ 
      if(!preg_match($zip_patt,$value)) 
      { 
	   $error_array[] = "$value is not a valid zip code";
       } //end of city check
	}
	if(preg_match("/phone/i",$field)) 
    { 
      if(!preg_match($phone_patt,$value)) 
      { 
        $error_array[] = "$value is not a valid phone number"; 
      } //end of phone check
	}
	if(preg_match("/email/i",$field)) 
    { 
      if(!preg_match($email_patt,$value)) 
      { 
        $error_array[] = "$value is not a valid email address"; 
      } //end of email check
    } 
  } 
  $clean_data[$field] = strip_tags(trim($value)); 
} 
if(@sizeof($error_array) > 0) 
{ 
  $message = "<ul style='color: red; list-style: none' >"; 
  foreach($error_array as $value) 
  { 
    $message .= "<li>$value</li>"; 
  } 
  $message .= "</ul>"; 
  echo $message; 
  extract($clean_data); 
  include("register_form5.inc"); 
  exit(); 
} 
else 
{ 
$cxn = mysqli_connect($host,$user,$passwd,$dbname) 
             or die("Couldn't connect to server"); 
foreach($clean_data as $field => $value) 
{ 
  $clean_data[$field] = mysqli_real_escape_string($cxn,$value); 
} 
$sql = "INSERT INTO customerData (user_name,password,first_name,last_name,address,address2,city,state,zip,country,phone,email) 
   VALUES ('$clean_data[user_name]','$clean_data[password]',
		   '$clean_data[first_name]','$clean_data[last_name]',
		   '$clean_data[address]','$clean_data[address2]',
		   '$clean_data[city]','$clean_data[state]',
		   '$clean_data[zip]','$clean_data[country]',
           '$clean_data[phone]','$clean_data[email]')"; 
$result = mysqli_query($cxn,$sql) 
            or die("Couldn't execute query"); 
include("regcomplete.inc"); 
}
} 
else 
{ 
  include("register_form5.inc"); 
} 
?>
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
I don't see anywhere in the code where you are telling the site to use a different value than default. When you ouput the <option> tag that is the one you want selected, you have to add the attribute selected="selected" <option selected="selected" value="..... />
 
Top