简体   繁体   中英

Contact form validation in PHP redirects to blank page

when my form is submitted it goes to the 'action' page, however its blank. And even though I have checks for email, name etc. It does not show any errors on that blank page. Been trying to get this to work for past 2 days.

Here is my php code:

<?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");
    }
}

So I put a HTML as well hope it helps you to help me haha And this is my 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>

Make sure error reporting is enabled. You can add this code to the top of the page.

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

There is your problem:

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

If you want to checking submit you should add a name and value like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM