【问题标题】:Contact form validation in PHP redirects to blank pagePHP中的联系表单验证重定向到空白页面
【发布时间】:2021-09-26 06:34:29
【问题描述】:

提交我的表单后,它会转到“操作”页面,但它是空白的。即使我检查了电子邮件、姓名等。它也没有在那个空白页上显示任何错误。 过去 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>

【问题讨论】:

    标签: php validation contact-form


    【解决方案1】:

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

    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>
    

    【讨论】:

    • 此外,如果您检查$_POST['submit'],检查!empty($_POST) 将毫无意义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2018-09-30
    • 1970-01-01
    相关资源
    最近更新 更多