dongqin1075 2016-12-03 23:13
浏览 67
已采纳

空白表单不断提交(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.";
}

?>

  • 写回答

1条回答 默认 最新

  • doudong7256 2016-12-04 15:36
    关注

    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;
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应