PHP contact form validation script not working

Posted on 2011-10-30
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
    LVL 82

    Expert Comment

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

    Accepted Solution

    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

    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

    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!
    LVL 4

    Expert Comment

    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

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now