【问题标题】:Form Validator for empty fields空字段的表单验证器
【发布时间】:2014-01-22 15:42:30
【问题描述】:

我在空白字段中遇到表单验证问题。如果 php 中有错误,我已经包含了代码,并且真的认为可以解决问题,但它仍然提交表单,无论如何并重定向到感谢页面。

表单开启:http://www.softwaretestingconference.com/try/register.html

这里是php:

<?php
$firstnameErr = $lastnameErr = $positionErr = $companynameErr = $address1Err = $postcodeErr = $countryErr =  $emailErr = $numberErr = $elecErr = $howManyErr;
$favFruit = array();
$myemail = 'tina.harris@31media.co.uk';//<-----Put Your email address here.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["firstname"])) {
        $firstnameErr = "Please provide your first name";
    }
    else {
        $firstname = $_POST["firstname"];
    }

    if (empty($_POST["lastname"])) {
        $lastnameErr = "Please provide your first name";
    }
    else {
        $lastname = $_POST["lastname"];
    }

    if (empty($_POST["position"])) {
        $positionErr = "Please state your job position";
    }
    else {
        $position = $_POST["position"];
    }
 if (empty($_POST["companyname"])) {
        $companynameErr = "Please state your company's name";
    }
    else {
        $companyname = $_POST["companyname"];
    }

    if (empty($_POST["address1"])) {
        $address1Err = "Please provide an address for your invoice";
    }
    else {
        $address1 = $_POST["address1"];
    }


 if (empty($_POST['address2'])) {
 }

        else {
        $address2 = $_POST['address2'];
    }

   if (empty($_POST["postcode"])) {
        $postcodeErr = "Please provide a postocde for your invoice";
    }
    else {
        $postcode1 = $_POST["postcode"];
    }


 if (empty($_POST["country"])) {
        $countryErr = "Please provide your country for your invoice";
    }
    else {
        $country = $_POST["country"];
    }


 if (empty($_POST["email"])) {
        $emailErr = "Please provide your email address";
    }
    else {
        $email = $_POST["email"];
    }


    if (empty($_POST["number"])) {
        $numberErr = "Please provide your phone number";
    }
    else {
        $number = $_POST["number"];
    }


    if (empty($_POST["elec"])) {
        $elecErr = "Please provide an electronic signature";
    }
    else {
        $elec = $_POST["elec"];
    }


}



if( empty($errors))
{
$to = $myemail;
$email_subject = "NSTC booking: $name";
$email_body = "You have received a new booking from NSTC. ".
" Here are the details:  \n Places1: $places1  \n Places2: $places2 \n Places3: $places3 \n Title: $title \n First name: $firstname \n Last name: $lastname \n Position: $position \n Company name: $companyname \n Address1: $address1 \n Address2: $address2  \n Email: $email \n Number: \n $number \n Elec: $elec  \n ";

if(isset($_POST['formDoor'])){
  if (is_array($_POST['formDoor'])) {
    foreach($_POST['formDoor'] as $value){
      $email_body .= $value;
    }
  } else {
    $value = $_POST['formDoor'];
    $email_body .= $value;

  }
}

$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: register-thanks.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

【问题讨论】:

    标签: html forms validation field


    【解决方案1】:

    您的问题在以下行(我的假设):

    if( empty($errors))
    

    您没有在 $errors 变量中设置任何内容。

    我会这样做(我会给你一个例子,然后你必须将它应用到你的每一个 if 语句。)

    在脚本的开头,定义一个空数组:

    $errors = array();
    

    然后(这是您必须在所有“if”语句中复制的内容:

    if (empty($_POST["email"])) {
        $errors['mailError'] = "Please provide your email address";
    }
    else {
        $email = $_POST["email"];
    }
    

    在脚本的最后:

    if (count($errors) === 0) {
        //passed, no errors.
    } else {
        //there are errors. handle them.
    }
    

    【讨论】:

    • 谢谢,虽然我试过了,点击提交后它只是说'数组',页面的其余部分是空的
    • 我想我对最后一部分还不够清楚,这里是为了理解它:)jsfiddle.net/L85ce/2
    • 非常感谢,它在 Firefox 和 Chrome 中运行良好,但在 Safari 中却不行,它只是让用户进入下一步......有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多