Displaying PHP Form Validation Error Messages on the Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tad-20
Forum Newbie
Posts: 2
Joined: Fri Feb 22, 2008 10:26 am

Displaying PHP Form Validation Error Messages on the Form

Post by tad-20 »

Hi.

I'm a complete PHP novice, and started getting into using PHP to process contact forms on websites. I currently have a PHP Form Wizzard, whereby you load the HTML form and it generates the php script to process the form and send it to an email address.

One of the options you can have is to display custom validation error messages.

However, currently, when you attempt to submit the form, the validation message loads on a blank page, thus you have to hit the back button in the browser window to go back to the page with the form.

What I would like to try and implement is that any validation error messages are displayed near the form or next to the relevant fields on the form page itself.

Perhaps this may be achieved by a php echo function?? I have copied the processing script part into this post so you can see where it generates the error messages. Your help really is much appreciated. :D

Many thanks,

Thomas.

PS. Please do let me know if you need any clarification or extra info. I may have been a bit vague!

Code: Select all

<?php
# ----------------------------------------------------
# -----
# ----- This script was generated by PHP-Form Wizard 1.2.6 on 04/07/2009 at 01:26:02
# -----
# ----- http://www.tools4php.com
# -----
# ----------------------------------------------------
 
 
// Receiving variables
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$name = addslashes($_POST['name']);
@$email = addslashes($_POST['email']);
@$subject = addslashes($_POST['subject']);
@$message = addslashes($_POST['message']);
@$reset = addslashes($_POST['reset']);
 
// Validation
if (strlen($name) <2)
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter your name</font></p>");
}
if (strlen($name) >50)
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter your name</font></p>");
}
 
if (strlen($name) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter your name</font></p>");
}
 
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter a valid email address</font></p>");
}
 
if (strlen($email) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter a valid email address</font></p>");
}
 
if (strlen($subject) <3)
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter a subject</font></p>");
}
if (strlen($subject) >80)
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter a subject</font></p>");
}
 
if (strlen($subject) == 0 )
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter a subject</font></p>");
}
 
if (strlen($message) <0)
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter a valid message</font></p>");
}
if (strlen($message) >700)
{
die("<p align='center'><font face='Arial' size='2' color='#FF0000'>Please enter a valid message</font></p>");
}
 
//Sending Email to form owner
$pfw_header = "From: $email\n"
  . "Reply-To: $email\n";
$pfw_subject = "$subject";
$pfw_email_to = "info@abc.co.uk";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "From: $name\n"
. "Email: $email\n"
. "\n"
. "Message: $message\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
 
//Sending auto respond Email to visitor
$pfw_header = "From: info@abc.co.uk\n"
  . "Reply-To: info@abc.co.uk\n";
$pfw_subject = "Thank you for your message - MGPS Services";
$pfw_email_to = "$email";
$pfw_message = "Dear $name,\n"
. "\n"
. "Thank you for contacting ABC Company, we have received your message and you will receive a reply from us soon to your email address of: $email\n"
. "\n"
. "Kind Regards,\n"
. "\n"
. "\n"
. "ABC Company\n"
. "www.abc.com";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
 
 echo("<p align='center'><font face='Arial' size='2' color='#808080'>Thank you for sending your message. You will hear from us shortly.</font></p>");
?>
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: Displaying PHP Form Validation Error Messages on the Form

Post by akuji36 »

Try to abandon your wizard and follow these php videos link

Take a look at lesson 7.

link here: http://phpvideotutorials.com/free

It will introduce you to regex --regular expressions

which are very useful for form validation.

Also consider downloading regexbuddy at :http://www.regexbuddy.com/.

Another helpful tool which will actually output code for you and eliminate hours
of searching for that one slice of php code that's wrecking your expected output.
Post Reply