2014-01-22 133 views
0

我在空字段中有表单验证问题。我已经包含了代码,如果有一个错误的PHP和真的认为这会做的伎俩,但它仍然提交表单,无论重定向到一个感谢页面。空字段的表单验证器

形式是:http://www.softwaretestingconference.com/try/register.html

这里是PHP:

<?php 
$firstnameErr = $lastnameErr = $positionErr = $companynameErr = $address1Err = $postcodeErr = $countryErr = $emailErr = $numberErr = $elecErr = $howManyErr; 
$favFruit = array(); 
$myemail = '[email protected]';//<-----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

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

if(empty($errors)) 

您没有设置任何东西在$ errors变量中。

我会做如下(我给大家举一个例子,你就必须把它应用到你的if语句中的每一个。)

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

$errors = array(); 

,然后(这是你会在你的所有的“如果”语句复制的内容:

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. 
} 
+0

她仍然仍然可以使用'如果(空($错误))' – ElendilTheTall

+0

谢谢你,虽然我尝试过了,它只是说,“阵”后,我点击提交,页面的其余部分是空的 – avamonroe

+0

伊夫做过现在所有,但它不工作:[libk](http://jsfiddle.net/L85ce/) – avamonroe