Form gives same error even when the form is completely filled out or not

Can someone help me. My php form is giving me the same error even when the form is completely filled out and even when a field is not filled out. When the name field or any field is not filled out, it doesn’t show my error message that says: The First Name you entered does not appear to be valid.

Here is the error below I am getting on all the fields if left unfilled and even when the whole form is filled out:

We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

We are sorry, but there appears to be a problem with the form you submitted.

Please go back and fix these errors.

Here is the php code. What am I doing wrong in the code?

<?php function spamcheck($field) { // Sanitize e-mail address $field=filter_var($field, FILTER_SANITIZE_EMAIL); // Validate e-mail address if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } } ?> <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = ""; $email_subject = "Medisoft Classes Registration"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['company']) || !isset($_POST['address1']) || !isset($_POST['address2']) || !isset($_POST['city']) || !isset($_POST['state']) || !isset($_POST['telephone']) || !isset($_POST['email']) || !isset($_POST['ClassType'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $company = $_POST['company']; // not required $address1 = $_POST['addressl']; // required $address2 = $_POST['address2']; // not required $city = $_POST['city']; // required $state = $_POST['state']; // required $telephone = $_POST['telephone']; // required $email_from = $_POST['email']; // required $classtype = $_POST['ClassType']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($address1) < 2) { $error_message .= 'The Address you entered does not appear to be valid.
'; } if(strlen($city) < 2) { $error_message .= 'The City you entered does not appear to be valid.
'; } if(strlen($state) < 2) { $error_message .= 'The State you entered does not appear to be valid.
'; } if(strlen($telephone) < 2) { $error_message .= 'The Telephone number you entered does not appear to be valid.
'; } if(strlen($ClassType) < 2) { $error_message .= 'Please select the Class you wish to attend.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "first_name: ".clean_string($first_name)."\n"; $email_message .= "last_name: ".clean_string($last_name)."\n"; $email_message .= "company: ".clean_string($company)."\n"; $email_message .= "address1: ".clean_string($address1)."\n"; $email_message .= "address2: ".clean_string($address2)."\n"; $email_message .= "city: ".clean_string($city)."\n"; $email_message .= "state: ".clean_string($state)."\n"; $email_message .= "telephone: ".clean_string($telephone)."\n"; $email_message .= "email: ".clean_string($email_from)."\n"; $email_message .= "ClassType: ".clean_string($ClassType)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?>

Your registration form has been submitted. We will be in contact with you soon. Thank you!

To return to our HMS website click HERE.

<?php

}

?>

Here is the html form code:

Registration Form

		 <font size="2" face="Arial, Arial, Helvetica"><b>* Required field</b></font>
        
		<p>
		<font size="2" face="Arial, Arial, Helvetica">
                    <label for="first name">First Name</label>
					<br></font>
		<font face="Arial, Arial, Helvetica">
		<input type="text" name="first_name" size="25" tabindex="1"                         

style="border:1px solid rgb(29,77,138); font-size: 10pt; color: rgb(0,0,0); background-color:rgb(100,125,160);
"> *


Last Name

                    <br></font>             	 		
		<font face="Arial, Arial, Helvetica">
                    <input type="text" name="last_name" size="25" tabindex="2"                           

style="border:1px solid rgb(29,77,138); font-size: 10pt; color: rgb(0,0,0); background-color:rgb(100,125,160);
"> *



Company







Address1



*



Address2







City



*



State



*



Telephone



*



Email Address



*





Class I wish to attend: *



Medisoft Beginners Training




Medisoft Intermediate Training




Medisoft Advanced Training




Well, first, double-spacing of code just wastes our time. Also, all code MUST be placed into PHP tags so we can copy it for testing. Please make sure you press the PHP button and paste future code between the tags. Thanks.

Now, as you see form your output, it did not show the errors. Therefore, there must be a logic error in the way you validate your code. First, your logic is to create a function to display the error and then end. Next, you create a list of various validations. If errors occur, you call the routine. This seems a bit backwards. I am assuming that this was created from someone else’s example. We can help you fix it so it works, but, it would be better to start over with the validation section and create a more normal version.

Remove the “died()” routine as it is just confusing and is not needed. (Think top-down programming…)
Next, handle your error messages and validation in a different order.
You currently check for empty, grab the field, validate it, then clean it again.
This is double work, no need to clean it if you already checked it or you could also
clean it when you grab it. I feel that the clean part is not needed…
You also build an error report and then jump back to a routine to display it. Waste of time.

Here is one way to handle it… (I will show two or three fields so you get the idea)

[php]
//Empty out the error message (Reset to nothing…)
$errors="";

//Start validation checks (Handle each input field separately as grouping them together is complicated
if ( !isset($_POST[“first_name”] ) {
$errors .= “First name is missing!
”;
} else {
// Name is not missing, validate it…
$first_name = $_POST[“first_name”];
if(!preg_match("/^[A-Za-z .’-]+$/",$first_name)) { $errors .= “Your first name does not appear to be valid!
”; }
}

if ( !isset($_POST[“last_name”] ) {
$errors .= “Last name is missing!
”;
} else {
// Name is not missing, validate it…
$last_name = $_POST[“last_name”];
if(!preg_match("/^[A-Za-z .’-]+$/",$last_name)) { $errors .= “Your last name does not appear to be valid!
”; }
}

if ( !isset($_POST[“email”] ) {
$errors .= “Your email address is missing!
”;
} else {
// Email is not missing, validate it…
$email = $POST[“email”];
if(!preg_match("/^[A-Za-z0-9.
%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/", $email)) { $errors .= “Your email address does not appear to be valid!
”; }
}

// continue with other fields…

// End of validation, if bad say so, if good store info in database…
if ( $errors == “” ) {
die(“Your form entries were in error! Please fix the following problems:
” . $errors);
} else {
//Here handle saving your data and displaying your “thank-you” message…
}
[/php]
Not sure if that will help ore make it more complicated… But, if you want us to fix your version we can if it is easier for you. Also, do not forget to test every single field with various inputs. Later on you should take the posted values and print back into the form. In this manner, people will not have to retype fields that have been okayed.

Let us know how you do with it. Good luck…

Hi, ErnieAlex, thank you so much for your help! The form is working.

Great! Always nice when you solve a programming issue. Glad I could help.

I will mark this one solved… Have a nice day!

Sponsor our Newsletter | Privacy Policy | Terms of Service