doushupu2521 2015-01-11 20:26
浏览 25

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

I have created an enquiry form which allows the user to submit a query and then be emailed a confirmation. That works ok however the last part of the validation does not seem to work so an email is being sent when it shouldn't be.

The data the form is acquiring is the Name, Email and a security question which is very simply to enter a number between 1 & 9. Letters are not accepted as the form has been set to "number" using HTML, however it does accept the number 0 & it also appears to accept any number over the value of 9.

I'm fairly certain my IF statement is incorrect however can't figure out what is wrong. Any help would be greatly appreciated.

The code

<?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' . "
" .
                       'Reply-To: webmaster@jamesvause.co.uk' . "
" .
                       '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>

Many Thanks for your help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录