繁体   English   中英

PHP-表单字段验证

[英]PHP - Form field validation

我有一个接受数据的表格,一旦用户填写了所有必填字段,我就使用php将其发送到我的电子邮件中。 如果一个字段为空,我会收到一条消息,例如。 "Email is required"但是电子邮件仍然发送。 我不知道问题出在哪里? 如果任何字段为空,我都不希望发送电子邮件。我也不希望每次单击提交时都刷新页面,我只想显示"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>

在检查完错误并加载错误消息变量之后,您很简单地发送电子邮件,而无需检查是否发现了任何错误。

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

首先更改此行以将错误变量设置为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");
}

该代码可在我自己的网站上运行,该代码段用于向您发送电子邮件,并且用户实际上没有进行验证以检查您的支票中是否出现任何错误。

<?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>

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

首先,解决您的问题的方法是,即使您捕获了错误

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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