dss89001 2018-11-13 05:53
浏览 68

PHP表单验证不起作用,允许发送无效的提交

I have written a script mail.php to handle the validation of form data and to send the form data via email. I have been testing this script by entering known invalid data however the script does not echo the error messages and just sends the data anyway.

Here is the form html:

<form method="post" action="mail.php" class="col-12" id="form-book">
            <div class="row">
              <div class=" form-group col-12">
                <p>Fill out the form below to tell me about your problem. <strong>Any problems to do with technology, all the solutions.</strong> Just tell me what you can, however the more info you give the better.</p>
              </div>
              <div class="form-group col-6">
                <label></label>
                <input id="name" name="name" placeholder="Name" type="text" required="required" class="form-control here bottom"> <span class="error"> <?php echo $nameErr; echo $nameErrEmpty;?></span>
              </div>
              <div class="form-group col-6">
                <label></label>
                <input id="phone" name="phone" placeholder="Phone#" type="text" required="required" class="form-control here bottom"> <span class="error"> <?php echo $phoneErr; echo$phoneErrEmpty;?></span>
              </div>
              <div class="form-group col-12">
                <label></label>
                <input id="email" name="email" placeholder="Email (Optional)" type="text" class="form-control here bottom"> <span class="error"> <?php echo $emailErr; echo $emailErrEmpty;?></span>
              </div>
              <div class="form-group col-6">
                <input data-provide="datepicker" id="date" name="date" placeholder="Pick a date" required="required" class="form-control here bottom"> <span class="error"> <?php echo $dateErr; echo $dateErrEmpty;?></span>
              </div>
              <div class="form-group col-6">
                <input type="text" id="time" name="time" placeholder="Choose the best time" required="required" class="form-control here bottom timepicker"> <span class="error"> <?php echo $timeErr; echo $timeErrEmpty;?></span>
              </div>
              <div class="form-group col-12">
                <label></label>
                <textarea id="message" name="message" cols="40" rows="5" class="form-control" required="required" aria-describedby="messageHelpBlock"></textarea> <span class="error"> <?php echo $messageErr; echo $messageErrEmpty;?></span>
                <span id="messageHelpBlock" class="form-text text-muted">Tell me a little about whats going on or what you need help with.</span>
              </div>
              <div class="form-group col-12">
                <button name="submit" type="submit" class="btn btn-primary">Send your message</button>
              </div>
            </div>
            <!--row-->
          </form>

Here is mail.php:

        <?php
    error_reporting(-1);
    ini_set('display_errors', 'On');
    set_error_handler("var_dump");

    date_default_timezone_set("Pacific/Honolulu");

    if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $date = $_POST['date'];
    $time = $_POST['time'];
    $message = $_POST['message'];
    $subject = "Appointment Booked";

    $mailTo = "itguy@johnpuaoi.com";

    $email = filter_var($email, FILTER_SANITIZE_EMAIL);

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }


    if (empty($name)) {
    $nameErrEmpty = "Your name is required";
    } elseif (is_int($name)) {
    $nameErr = "That is not a valid name";
    } else {
    $nameTested = test_input($name);
    }

    if (empty($phone)) {
    $phoneErrEmpty = "Your phone number is required";
    } elseif (is_int($name)) {
    $phoneTested = test_input($phone);
    } else {
    $phoneErr = "Phone Number needs to be in this format e.x 1234567890";
    }

    if (empty($email)) {
    $emailErrEmpty = "Your name is required";
    } elseif (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $emailErr = "That is not a valid email";
    } else {
    $emailTested = test_input($name);
    }

    if (empty($message)) {
    $messageErrEmpty ="You must tell me about your problem";
    } else {
    $messageTested = test_input($message);
    }


    $dateTested = test_input($date);
    $timeTested = test_input($time);

    $headers = "From: ".$emailTested;
    $txt = "An appointment was booked by ".$nameTested.".

".$dateTested." @".$timeTested.".

".$messageTested;

    mail($mailTo, $subject, $txt, $headers);
    header("Location: index.php?mailsend");
    }
    ?>

I have searched stack for similar issues but have found none. I am a total noob to php so any help would be appreciated. Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏