Preg_match下拉选项?

时间:2014-09-28 09:03:52

标签: php

我有3个下拉框。一个是男性或女性,另一个是国家,另一个是国家。

如果没有选择,我该怎么做错?

目前我没有收到任何错误,因为它在字段框中预先填写了单词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");
            }
        }
    }

1 个答案:

答案 0 :(得分: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";
相关问题