PHP,回显'1'或不回显指定的值

时间:2015-05-13 17:05:51

标签: php forms

我创建了一个简单的表单验证,在value属性中使用PHP代码自动键入表单,名称和注释显示正确期望email(输入类型=“文本“)。我正确编写了代码,或者我的代码有任何代码味道。

编辑:我的问题不是那个,我没有得到一个主要的是 我得到一个值为1或该值被清除。

PHP代码: -

<?php 

if($_SERVER['REQUEST_METHOD']=='POST') {
    $error="";
    if(strlen ( $_POST['name'] ) < 5 ) {
        $error = "Please type more than 4 characters<br/>";
    }

    if ( $_POST['email']="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){
                $error.= "Please type a valid Email<br/>";
    }

    if(strlen($_POST['comment']) < 4){
        $error.= "Please type more than 4 characters";
    }   

    if( !empty($error) ){
        $result = "<div class='alert alert-danger'>$error</div>";
    }   

    else {

        if( mail('jokersspirit@gmail.com','test message',"Name:".$_POST['name'].
            "Email:".$_POST['email'].
            "Comment:".$_POST['comment']) ){

            $result = "<div class='alert alert-success'>Your form has been submitted</div>";
        }

        else{
            $result = "<div class='alert alert-success'>Error occurred 
            while submitting your form please try again later</div>";
        }


}

}


 ?>

HTML代码: -

<form action="" method="post">

        <label for="name">Your Name:</label>
        <input type="text" class="form-control" name="name" id="name" placeholder="name" value="<?php echo isset($_POST['name'])?$_POST['name']:""; ?>">

        <label for="email">Your Email:</label>
        <input type="text" class="form-control" name="email" id="email" placeholder="email" value="<?php echo isset($_POST['email'])? $_POST['email']:""; ?>">

        <label for="comment">Your Comments:</label>
        <textarea name="comment" placeholder="comments" class="form-control" id="comment">
        <?php echo isset($_POST['comment'])?$_POST['comment']:""; ?>
        </textarea>

        <input type="submit" class="btn btn-success btn-lg " value="Submit">
    </form>

1 个答案:

答案 0 :(得分:1)

您的错误在这里

if ( $_POST['email']="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){

将其替换为:

if ( $_POST['email']=="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){

=替换为==

没有它,你的$_POST['email']是一个空字符串。