2014-09-28 83 views
-1

我有3個下拉框。一個是男性或女性,另一個是國家,另一個是國家。Pregmatch下拉選項?

如果沒有被選中,我該如何爲此做出錯誤?

目前我沒有得到任何錯誤,因爲它預先填寫了字段框中的單詞select。

if (!$_POST['gender']) $error.="<br />Please enter your gender"; 

    if (!$_POST['country']) $error.="<br />Please enter your country"; 

    if (!$_POST['state']) $error.="<br />Please enter your state"; 

HTML代碼格式:

<form class="form-horizontal" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">First Name*</label> 
       <div class="col-md-2"> 
        <input type="text" name="firstname" id="firstname" class="form-control input-md" value="<?php echo addslashes ($_POST['firstname']); ?>" /> 
       </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">Last Name*</label> 
       <div class="col-md-2"> 
        <input type="text" name="lastname" class="form-control input-md" value="<?php echo addslashes ($_POST['lastname']); ?>" /> 
       </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">Email*</label> 
       <div class="col-md-2"> 
        <input type="email" name="email" class="form-control input-md" value="<?php echo addslashes ($_POST['email']); ?>" /> 
       </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">Password*</label> 
       <div class="col-md-2"> 
        <input type="password" name="password" class="form-control input-md" /> 
       </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">Confirm Password*</label> 
       <div class="col-md-2"> 
        <input type="password" name="confirmpassword" class="form-control input-md" /> 
       </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">Gender*</label> 
       <div class="col-md-1"> 
        <select id="gender" name="gender" class="form-control"> 
         <option>Select</option> 
         <option>Male</option> 
         <option>Female</option> 
        </select> 
       </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">Country*</label> 
       <div class="col-md-2"> 
        <select id="country" name ="country" class="form-control"></select> 
       </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-md-5 control-label">State*</label> 
       <div class="col-md-2">  
        <select name ="state" id ="state" class="form-control"></select> 
         <script language="javascript"> 
         populateCountries("country", "state"); 
         </script> 
       </div> 
     </div> 

      <div class="text-center"> 
       <input type="submit" name="submit" class="btn btn-kani btn-lg" value="Sign Up"/>     
      </div> 


    </form> 

整個代碼:

include 'connection.php';  
    if ($_POST['submit']=="Sign Up") { 
     if (!$_POST['firstname']) $error.="<br />Please enter your first name"; 
      else {   
       if (!preg_match("/^[a-zA-Z]+$/", $_POST["firstname"])) $error.="<br />First name may only contain letters";   
      } 
     if (!$_POST['lastname']) $error.="<br />Please enter your last name"; 
      else {   
       if (!preg_match("/^[a-zA-Z]+$/", $_POST["lastname"])) $error.="<br />Last name may only contain letters";   
      } 
     if (!$_POST['email']) $error.="<br />Please enter your email"; 
      else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $error.="<br />Please enter a valid email address";   
     if (!$_POST['password']) $error.="<br />Please enter your password"; 
      else {   
       if (strlen($_POST['password'])<8) $error.="<br />Please enter a password with at least 8 characters"; 
       if (!preg_match('`[A-Z]`', $_POST['password'])) $error.="<br />Please enter at least 1 capital letter";   
      } 
     if ($_POST['password'] !== $_POST['confirmpassword']) 
      $error.="<br />Your passwords do not match."; 
     if (!$_POST['gender']) $error.="<br />Please enter your gender"; 
     if (!$_POST['country']) $error.="<br />Please enter your country"; 
     if (!$_POST['state']) $error.="<br />Please enter your state";   

     if ($error) $error = "<strong>There were error(s) in your signup details:</strong><br />".$error; 
     else { 
      $query = "SELECT * FROM `users` WHERE email='".mysqli_real_escape_string($link, $_POST['email'])."'"; 
      $result = mysqli_query($link, $query); 
      $results = mysqli_num_rows($result); 
      if ($results) $error = "That email address is already registered"; 
      else { 
       $query="INSERT INTO `users` (`firstname`, `lastname`,`email`, `password`, `gender`, `country`, `state`) VALUES('".mysqli_real_escape_string($link, $_POST['email'])."', '".md5(md5($_POST['email']).$_POST['password'])."')"; 
       mysqli_query($link, $query); 
       $_SESSION['id']=mysqli_insert_id($link); 
       header("Location: dashboard.php"); 
      } 
     } 
    } 
+0

你能顯示你的html代碼嗎? ? – 2014-09-28 09:09:09

回答

0

你需要檢查與isset()函數的功能。

if (!isset($_POST['gender'])) 
    $error.="<br />Please enter your gender"; 
if (!isset($_POST['country'])) 
    $error.="<br />Please enter your country"; 
if (!isset($_POST['state'])) 
    $error.="<br />Please enter your state"; 
+0

仍然不會顯示任何有關未選擇選項的錯誤 – 2014-09-28 09:18:37