PHP - 表单字段验证

时间:2017-06-13 10:43:43

标签: php html5 email

我有一个接收数据的表单,一旦用户填写了所有必填字段,我就会使用php将其发送到我的电子邮箱。如果一个字段是空的,我会收到一条消息,例如。 "Email is required",但电子邮件仍然发送。我不知道问题是什么? Idont想要发送电子邮件,如果任何字段为空我也不想每次点击提交时刷新页面,我想只显示"Required message".

<?php
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = "";
$first_name = $last_name = $email = $ironing = $description = $Rooms ="";

if(isset($_POST['submit'])){
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $ironing = $_POST['ironing'];
    $Rooms = $_POST['Rooms'];
    $Description = $_POST['description'];
    if (empty($_POST["first_name"])) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST["first_name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed"; 
        }
    }
    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format"; 
        }
    }

    if (empty($_POST["description"])) {
        $descriptionErr = "Description is required";
    } else {
        $description = test_input($_POST["description"]);
    }
    if (empty($_POST["Rooms"])) {
        $RoomErr = "Room number is Required";
    } else {
        $Rooms = test_input($_POST["Rooms"]);
    }
    if (empty($_POST["ironing"])) {
        $ironingErr = "Ironing is Required";
    } else  {
        $ironing = test_input($_POST["ironing"]);
    }

    $to = "someemail@gmail.com"; // this is your Email address
    $subject = "Order Sumbittion";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". "\n\n"  . $_POST['Rooms'] ."Ironing: " . $_POST['ironing'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . "Number of Rooms: " . $_POST['Rooms'] ."Ironing: ". $_POST['ironing'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2);          
    // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    header("Location: index.php");
}
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<p><span class="error">* required field.</span></p>
<div class="col-md-9">
    <form action="" method="post">
        First Name: <input type="text" name="first_name">
        <span class="error">* <?php echo $nameErr;?></span><br>
        <br>
        Last Name: <input type="text" name="last_name">
        <span class="error">* <?php echo $lastNameErr;?></span><br>
        Email:
        <br>
        <input type="text" name="email">
        <span class="error">* <?php echo $emailErr;?></span>
        <br>
        Ironing?<br>
        <input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="Yes") echo "checked";?> value="Yes">Yes
        <input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="No") echo "checked";?> value="No">No
        <span class="error">* <?php echo $ironingErr;?></span>
        <br>
        Number Of Rooms:
        <br>
        <input type="text" name="Rooms">
        <span class="error">* <?php echo $RoomErr;?></span>
        <br>
        Description of the House:
        <br>
        <textarea name="description" rows="10" cols="70"></textarea>
        <span class="error">* <?php echo $descriptionErr;?></span>
        <br>
        <input type="submit" name="submit" value="Submit">
    </form>

4 个答案:

答案 0 :(得分:1)

在检查错误并加载错误消息变量之后,您只需发送电子邮件而不检查是否发现任何错误。

因此,在发送电子邮件之前尝试添加一些代码,以检查是否存在任何类似的错误,例如

首先更改此行以将错误变量设置为NULL

$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = NULL;

然后在这样的测试中包装发送的电子邮件

if (isset( $nameErr) || isset($lastNameErr) || isset($emailErr) ||
     isset($ironingErr) || isset($descriptionErr) || isset($RoomErr) ) {
    // You have an error
} else {
    $to = "someemail@gmail.com"; // this is your Email address
    $subject = "Order Sumbittion";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". "\n\n"  . $_POST['Rooms'] ."Ironing: " . $_POST['ironing'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . "Number of Rooms: " . $_POST['Rooms'] ."Ironing: ". $_POST['ironing'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2);          
    // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    header("Location: index.php");
}

答案 1 :(得分:1)

此代码适用于我自己的网站,用于自行发送电子邮件的代码块,用户实际上没有验证来检查您的支票是否出现任何错误。

<?php
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = "";
$first_name = $last_name = $email = $ironing = $description = $Rooms ="";

$error = false;

if(isset($_POST['submit']))
    {   
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $ironing = $_POST['ironing'];
    $Rooms = $_POST['Rooms'];
    $Description = $_POST['description'];

    if (empty($_POST["first_name"])) {
        $nameErr = "Name is required";
        $error = true;
    } else {
        $name = test_input($_POST["first_name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed"; 
            $error = true;
        }
    }
    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
         $error = true;
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format"; 
             $error = true;
        }
    }

    if (empty($_POST["description"])) {
        $descriptionErr = "Description is required";
         $error = true;
    } else {
        $description = test_input($_POST["description"]);
    }
    if (empty($_POST["Rooms"])) {
        $RoomErr = "Room number is Required";
         $error = true;
    } else {
        $Rooms = test_input($_POST["Rooms"]);
    }
    if (empty($_POST["ironing"])) {
        $ironingErr = "Ironing is Required";
         $error = true;
    } else  {
        $ironing = test_input($_POST["ironing"]);
    }

    if ($error === false)
        {
    $to = "youremail@gmail.com"; // this is your Email address
    $subject = "Order Sumbittion";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". "\n\n"  . $_POST['Rooms'] ."Ironing: " . $_POST['ironing'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . "Number of Rooms: " . $_POST['Rooms'] ."Ironing: ". $_POST['ironing'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2);          
    // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    header("Location: index.php");
        }
}
// You can also use header('Location: thank_you.php'); to redirect to another page.

?>
<p><span class="error">* required field.</span></p>
<div class="col-md-9">
    <form action="" method="post">
        First Name: <input type="text" name="first_name">
        <span class="error">* <?php echo $nameErr;?></span><br>
        <br>
        Last Name: <input type="text" name="last_name">
        <span class="error">* <?php echo $lastNameErr;?></span><br>
        Email:
        <br>
        <input type="text" name="email">
        <span class="error">* <?php echo $emailErr;?></span>
        <br>
        Ironing?<br>
        <input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="Yes") echo "checked";?> value="Yes">Yes
        <input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="No") echo "checked";?> value="No">No
        <span class="error">* <?php echo $ironingErr;?></span>
        <br>
        Number Of Rooms:
        <br>
        <input type="text" name="Rooms">
        <span class="error">* <?php echo $RoomErr;?></span>
        <br>
        Description of the House:
        <br>
        <textarea name="description" rows="10" cols="70"></textarea>
        <span class="error">* <?php echo $descriptionErr;?></span>
        <br>
        <input type="submit" name="submit" value="Submit">
    </form>

答案 2 :(得分:0)

如果您不想刷新页面,则可以使用ajax调用在服务器上发送数据进行验证。否则表格将提交,每次您提交时页面都会刷新。 每次天气数据有效时都会发送电子邮件,因为没有条件检查数据是否有效。因此,使用变量并将其指定为“false”,然后在发送检查之前是否仍为真,然后发送电子邮件。                  }

答案 3 :(得分:0)

首先,您的问题的解决方案是即使您发现了错误

if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } 

您没有应用任何检查以确保脚本执行不会继续,为此您可以添加die();如果发现任何错误,您可以将状态变量设为$status = 0; {1}}并在发送电子邮件之前检查$status = 1。 现在,如果您想在不刷新页面的情况下显示错误消息,我建议使用jquery或任何插件,例如https://validatejs.org/