PHP contact form validation script not working

Posted on 2011-10-30
Medium Priority
Last Modified: 2012-05-12
I want my contact.php page to validate whether all fields have been completed on a feedback form and to check the validity of an email address entry.

The validation script won't work and I have tried every conceivable amendment to try and get it to this weekend, and now I need to get some sleep as it is 1am in the UK!

At present the page is showing up on my browser so there can't be any serious issues with the PHP entered in terms of syntax, however as soon as the page loads I receive the "mandatory fields missing" error message.  Other times when I changed the code I would receive the "mandatory fields missing" error message correctly after missing a field's content but when I entered data in all fields with an incorrect email address (no @ sign), the fields would just go blank and nothing is relayed back to the user making me think that there is also an issue with the email validation script, however I have used this script before and it works fine.

Any help would be truly appreciated as I have a deadline of the end of this week to meet for the overall project and the issue is putting a serious dent in my progress!

Many thanks in advance. I attach the relevant div's script containing PHP mailing code and the relevant HTML form Erroneous-contact-PHP.txt
Question by:CDIT_Solutions
  • 2
  • 2
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37054653
The routines that actually do the checking do not appear to be in that file.  Are you sure you included them on that page?

Accepted Solution

happysunny earned 2000 total points
ID: 37054661
A few things that stand out to me...

You have this:
		// form data
		$contactsenderName = strip_tags($_POST['contactusername']);
  		$contactsenderEmail = strtolower(strip_tags($_POST['contactemail']));
  	  	$contactsenderMessage = stripslashes(strip_tags($_POST['contactmessage']));
		$contacterrormandatory = ""; // default value of error string
		$contacterroremailvalid = ""; // default value of error string
		$contactsenderName = "";  // default value of string
		$contactsenderEmail = "";  // default value of string
		$contactsenderMessage = "";  // default value of string

Open in new window

The latter $contactsenderName, $contactsenderEmail, and $contactsenderMessage, are wiping out the first variables.  So when you "//check for existence", it sees the latter ones of no value.  Does that make sense?

Also, further down you have this:
$contacterrormandatory="<strong>!</strong>There are mandatory fields missing, please retry";

Open in new window

and it should be this:
$contacterrormandatory="<strong>!</strong>There are mandatory fields missing, please retry";

Open in new window

Hope that helps :)

Author Comment

ID: 37054689
Thanks very much for the tips, I will try later today as I must get to bed or else there is no point sleeping tonight lol!

Author Closing Comment

ID: 37055664
Thanks so much for your help with this, I removed the lines that were wiping out the variables and included the "curly brackets" around the else clause and it worked.

I am very grateful, next time I will ask quicker instead of trying 101 different variations first lol!

Expert Comment

ID: 37057327
LOL  I'm the same way!  I want to make sure I've exhausted every possible way before I ask for help :)  Glad it worked for you!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question