表单验证器为空字段

时间:2014-01-22 15:42:30

标签: html forms validation field

我在空字段中遇到表单验证问题。我已经包含了代码,如果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>

1 个答案:

答案 0 :(得分: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.
}
相关问题