空白表单不断提交(HTML,PHP)

空白表单不断提交(HTML,PHP)

问题描述:

So I have a contact form where customers can fill in their details. When submitted, the form posts the information across to a PHP script that then formats it into html and emails it to my business email.

The only problem I have is that I keep receiving blank emails at random. Obviously the PHP script is being triggered and an email is being sent but I'm not sure why.

The form has required fields so even if someone tried to submit it blank, it wouldnt let them. When I recieve these emails, there should at least be something in them.

I've thought about adding some extra validation to the PHP script to check if any of the required values are empty/missing, but the form deals with this anyway.

Does anyone know what is happening? It's probably something simple i've overlooked.

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$budget = $_POST['budget'];
$timeframe = $_POST['timeframe'];
$desc = $_POST['desc'];

//Send HTML formatted email
$send = mail("nathan@nathanthompson.co.uk",

 "You have received an enquiry",

  "<html>
    <body>
        <h3>Here is the information for the enquiry: </h3>
        <br>
        <p>Name: $name</p>
        <p>Email: $email</p>
        <p>Budget: $budget</p>
        <p>Timeframe: $timeframe</p>
        <br>
        <p>Description of enquiry:</p>
        <p>$desc</p>
    </body>
    </html>
  ", "Content-type: text/html; charset=iso-8859-1");

if ($send)
{
    header("Location:index.html");
}
else
{
    echo "An error occurred. Please return to the contact form and try again.";
}

?>

Here you go, try this! :)

<?php
    //Check the request method
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //Start by importing and sanitizing the fields
        $name = sanitize($_POST["name"]);
        $email = sanitize($_POST["email"]);
        $budget = sanitize($_POST["budget"]);
        $timeframe = sanitize($_POST["timeframe"]);
        $desc = sanitize($_POST["desc"]);

        //Make sure the fields are populated
        if (empty($name)) {die("The &#39;Name&#39; field was not populated. Please return to the contact form and try again.");}
        if (empty($email)) {die("The &#39;Email&#39; field was not populated. Please return to the contact form and try again.");}
        if (empty($budget)) {die("The &#39;Budget&#39; field was not populated. Please return to the contact form and try again.");}
        if (empty($timeframe)) {die("The &#39;Time Frame&#39; field was not populated. Please return to the contact form and try again.");}
        if (empty($desc)) {die("The &#39;Description&#39; field was not populated. Please return to the contact form and try again.");}

        //Everthing is fine, so send the email!
        $send = mail("nathan@nathanthompson.co.uk", "You have received an enquiry",
        "<html>
            <body>
                <h3>Here is the information for the enquiry: </h3>
                <br>
                <p>Name: $name</p>
                <p>Email: $email</p>
                <p>Budget: $budget</p>
                <p>Timeframe: $timeframe</p>
                <br>
                <p>Description of enquiry:</p>
                <p>$desc</p>
            </body>
            </html>
          ", "Content-type: text/html; charset=iso-8859-1");

        if ($send) {
            header("Location: index.html");
        } else {
            die("An error occurred. Please return to the contact form and try again.");
        }
    } else {
        die("An error occurred. Please return to the contact form and try again.");
    }

    function sanitize($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data, ENT_QUOTES);
        return $data;
    }
?>