+ Reply to Thread
Results 1 to 4 of 4

Thread: Redisplaying a drop-down menu choice...

  1. #1
    jbdesign's Avatar
    jbdesign is offline x10Hosting Member jbdesign is an unknown quantity at this point
    Join Date
    Mar 2009
    Location
    Nampa, ID, USA
    Posts
    26

    Redisplaying a drop-down menu choice...

    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 by jbdesign; 06-01-2009 at 05:40 PM.

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Redisplaying a drop-down menu choice...

    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="..... />
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    jbdesign's Avatar
    jbdesign is offline x10Hosting Member jbdesign is an unknown quantity at this point
    Join Date
    Mar 2009
    Location
    Nampa, ID, USA
    Posts
    26

    Re: Redisplaying a drop-down menu choice...

    I got it working, thanks for your help (as always)!

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Redisplaying a drop-down menu choice...

    No problem
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread

Similar Threads

  1. Css drop down menu
    By vidhem in forum Programming Help
    Replies: 10
    Last Post: 12-12-2008, 01:33 PM
  2. Drop Down Menu -> Mysql
    By White-Fox in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 09-01-2008, 02:20 PM
  3. help!! coding advanced drop down menu
    By zynischen in forum Programming Help
    Replies: 3
    Last Post: 04-25-2008, 05:14 PM
  4. coding advanced drop down menu
    By zynischen in forum Programming Help
    Replies: 0
    Last Post: 04-20-2008, 04:13 AM
  5. Mobile phone Codes
    By greeting in forum Computers & Technology
    Replies: 10
    Last Post: 03-28-2008, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers