2017-06-13 156 views
-1

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

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 = "[email protected]"; // 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> 
+0

我建议在提交表单前在前端使用[Parsley javascript validation](http://parsleyjs.org/)。将是更好的用户体验,意味着您可以避免不必要地刷新页面。 – stevenkellow

+0

我想避免尽可能多的JavaScript,而不是它的粉丝。 –

+0

一些明智的代码缩进将是一个好主意。它可以帮助我们阅读代码,更重要的是,它可以帮助您**调试您的代码** [快速浏览编码标准](http://www.php-fig.org/psr/psr-2/ )为了您自己的利益。您可能会被要求在几周/几个月内修改此代码 ,最后您会感谢我。 – RiggsFolly

回答

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 = "[email protected]"; // 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"); 
} 
+0

上面的代码完全一样,发出错误,但电子邮件仍然发送。 –

+0

再次检查,我做了一个小模,以我的答案,你可能没有拿起 – RiggsFolly

+0

仍然是一样的。 –

0

如果你不想刷新页面,则你可以使用ajax调用来发送服务器上的数据来验证。否则,表单将提交,每次提交时都会刷新页面。 每次天气数据有效时都会发送电子邮件,因为没有条件检查数据是否有效。因此,使用一个变量并将其赋值为'false',并在发送前检查它是否仍然为真,然后发送电子邮件。第一 }

0

第一件事,解决你的问题是,即使你抓住了

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

你没有应用任何检查,以确保该脚本执行不下去的错误,为此,你可以添加die();如果您发现任何错误,只需指定$status = 1,并在发送电子邮件支票if($status == 0)之前,您也可以将状态变量作为$status = 0;。 现在,如果你想显示错误信息而无需刷新页面,我建议使用jquery或任何插件,如https://validatejs.org/

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 = "[email protected]"; // 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>