联系表格 - 如果声明不适用于验证

时间:2015-01-11 20:26:41

标签: php forms validation if-statement

我创建了一个查询表单,允许用户提交查询,然后通过电子邮件发送确认。这工作正常,但验证的最后一部分似乎不起作用,因此当它不应该发送时发送电子邮件。

表单获取的数据是名称,电子邮件和安全问题,非常简单地输入介于1和1之间的数字。 9.不接受信件,因为表格已设置为" number"使用HTML,但它确实接受数字0&它似乎也接受超过9的任何数字。

我相当肯定我的IF声明不正确,但无法弄清楚出了什么问题。任何帮助将不胜感激。

代码

<?php
    require_once __DIR__ . ('/../config/init.php');

    //Checks if data has been posted off .. if so run validation
    if($_POST){

        if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
            $response = '<h5>Error</h5><p>Please Enter a valid email address</p>';
        }

        else if (empty($_POST['query'])){
            $response = "<h5>Error</h5><p>You have not entered a query. Please go back and try again";
        }

        else if (empty($_POST['check']) || $_POST['check']<1 && $_POST['check']>=9){
            $response = "<h5>Error</h5><p>Number entered not valid. Please go back and try again</p>";
        }

        else{
            $message = 'Thank you for enquiry. We will contact you as soon as possible';
            $headers = 'From: webmaster@jamesvause.co.uk' . "\r\n" .
                       'Reply-To: webmaster@jamesvause.co.uk' . "\r\n" .
                       'X-Mailer: PHP/' . phpversion();
            mail($_POST['email'], 'Your Enquiry', $message, $headers);

            $response = '<h3>Thank you for your enquiry</h3>';
        }

    }
?>
<!DOCTYPE html>
<html>
<head>
    <?php 
        include INCLUDES . 'head_tags.php';
    ?>
</head>
<body>
    <div class='container'>
        <?php
            include INCLUDES . 'header.php';
            include INCLUDES . 'nav.php';
        ?>
        <div class='two-thirds column'>
            <h2>Contact</h2>
            <form action='#' method='post'>
                <div>
                    <?php 
                        if($_POST){
                            echo $response;
                        }
                    ?>  
                </div>  
                <div>
                    <label for="email">E-mail:</label>
                    <input type='email' id="email" name='email' placeholder='Your e-mail address' autocomplete='on' required>
                </div>
                <div>
                    <label for="query">Query:</label>
                    <input type='text' id="query" name='query' placeholder="Please enter your query" required>
                </div>
                <div>
                    <label for="check">Enter a number between 1 and 9:</label>
                    <input type='number' id="check" name='check' maxlength="1" required>
                </div>
                <input type='submit' value='submit'>
            </form>
        </div>
    </div>
    <?php
        include INCLUDES . 'footer.php';
    ?>
</body>
</html>

非常感谢您的帮助。

0 个答案:

没有答案