Reputation: 23
Im having issues with form validation in an empty field. I've included code if there is an error in the php and really thought that would do the trick, but it still submits the form, regardless and redirects to a thank you page.
The form is on: http://www.softwaretestingconference.com/try/register.html
Here is the 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>
Upvotes: 0
Views: 114
Reputation: 21759
Your problem is in the following line (My assumption):
if( empty($errors))
You are not setting anything in the $errors variable.
I would do as follows (I will give you one example, you then have to apply it to each one of your if statements.)
at the beginning of your script, define an empty array:
$errors = array();
and then (this is what you will have to replicate in all your "if" statements:
if (empty($_POST["email"])) {
$errors['mailError'] = "Please provide your email address";
}
else {
$email = $_POST["email"];
}
and at the end of your script:
if (count($errors) === 0) {
//passed, no errors.
} else {
//there are errors. handle them.
}
Upvotes: 1