Jump to content

Recommended Posts

I hope someone can help me out. I am fairly new to the PHP world, but getting the hang of it fairly quickly.

 

Right now I am working of this code below. The problem I am having is that I can't seem to figure out how to make it 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.

 

I know I need to include an if-statement, I just don't know where.

 

Any and all help would be greatly appreciated.

<?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"); 
} 
?>

it depends on how you have the dropdown coded.  Does it go through a loop displaying options from some query or something, or is it hardcoded?  If it is hardcoded, you need to make it looped by at the very least putting it into an array and looping through it.  That way in the loop that displays it, you would put something like this:

 

$selected = ($_POST['fieldname'])? "selected='selected'" : "";
echo "<option value='...' $selected> ... </option>";

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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