PHP 中的联系表单验证重定向到空白页面

Contact form validation in PHP redirects to blank page

提交我的表单后,它会转到 'action' 页面,但它是空白的。即使我检查了电子邮件、姓名等,它也没有在该空白页上显示任何错误。 过去 2 天一直在努力让它发挥作用。

这是我的 php 代码:

<?php

if (isset($_POST['submit']) && !empty($_POST)) {

    $myEmail = "xxxxxxxxxx"; //securyti XD
    $timeStamp = date("dd/M/YY HH:i:s");
    $body = "";

    $fullName = $_POST['fullName'];
    $customerEmail = $_POST['customerEmail'];
    $chosenSubject = $_POST['subject'];
    $message = $_POST['message'];

    $nameRegularExpression = "/^[a-zA-Z]+$/";
    $emailRegularExpression = "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/";

    $body .= "From: " . $fullName . " at $timeStamp" . "\r\n";
    $body .= "Email: " . $customerEmail . "\r\n";
    $body .= "Message: " . $message . "\r\n";

    if (!preg_match($nameRegularExpression, $fullName) or empty($fullName)) {
        echo "Your name is not filled out properly or not filled out at all";
    }

    if (!preg_match($emailRegularExpression, $customerEmail) or empty($customerEmail)) {
        echo "Your mail is not valid or you forgot to fill it out";
    }

    if (empty($chosenSubject) or empty($message)) {
        echo "Dont forget to choose subject or write a message";
    }

    else {
        mail($myEmail, $chosenSubject, $body);
        header("Location: mail_sent.php");
    }
}

所以我也放了一个HTML希望对你有帮助哈哈 这是我的 html:

<form action="/public/contact_form.php" method="POST">
                <label for="name">Full Name ✍</label> <br />
                <input
                    type="text"
                    id="fullName"
                    name="fullName"
                    placeholder="Full Name"
                    required
                />
                <br />
                <label for="yourEmail">Your Email </label> <br />
                <input
                    type="email"
                    id="customerEmail"
                    name="customerEmail"
                    placeholder="Your Email"
                    required
                />
                <br />
                <label for="subject"
                    >What is your reason for contacting us? </label
                >
                <br />
                <select id="subject" name="subject">
                    <option value="basic">Pick a subject</option>
                    <option value="general">I have a question</option>
                    <option value="collaboration">
                        I want to offer a collaboration
                    </option>
                    <option value="other">
                        I have something else on my mind
                    </option>
                </select>
                <br />
                <label for="message">Your message to us ‍</label> <br />
                <textarea
                    name="message"
                    id="message"
                    cols="40"
                    rows="6"
                ></textarea
                ><br />
                <div class="form-buttons">
                    <button type="submit" class="send">SEND</button>
                    <button type="reset" class="messup">Wait, I messed up...</button>
                </div>
            </form>

确保错误报告已启用。您可以将此代码添加到页面顶部。

ini_set('error_reporting', true);
error_reporting(E_ALL);

你的问题是:

<button type="submit" class="send">SEND</button>

如果你想检查提交,你应该像这样添加一个名称和值:

<button type="submit" name="submit" value="1" class="send">SEND</button>