Is anybody able to help me with form validation?

Hi there, after reading the W3Schools chapter on form validation and having no luck, I’m hoping somebody can point me in the right direction with what I am trying to achieve…

I am an apprentice web designer and my first client has asked for a basic form for his website.
This I have completed and the results are successfully being sent to my email inbox as a test. However my client has asked me to include validation with the form ( required fields and a valid email address)

I am hoping to get an error message printed next to the form label if there is an error with the email address or if a field has been left blank when the user clicks submit. I have seen this on other forms, where the error text appears in red…

If anybody can give me a push in the right direction on this it will be greatly appreciated, the code I am using in my webpage and the php are below

my form in html:
[php]

























First Name


Last name


Amount of Cover required


Home Phone Number


Email Address


 
[/php]

and my php is:

[php]<?php

$EmailFrom = "[email protected]";
$EmailTo = "[email protected]";
$Subject = "Online contact form";
$Firstname = Trim(stripslashes($_POST['Firstname'])); 
$Lastname = Trim(stripslashes($_POST['Lastname'])); 
$Coveramount = Trim(stripslashes($_POST['Coveramount'])); 
$Phonenumber = Trim(stripslashes($_POST['Phonenumber'])); 
$Email = Trim(stripslashes($_POST['Email'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://cm4u.co.uk.co.uk/Thankyou.html\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Firstname: ";
$Body .= $Firstname;
$Body .= "\n";
$Body .= "Lastname: ";
$Body .= $Lastname;
$Body .= "\n";
$Body .= "Coveramount: ";
$Body .= $Coveramount;
$Body .= "\n";
$Body .= "Phonenumber: ";
$Body .= $Phonenumber;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://cm4u.co.uk.co.uk/FinanceForms/Thankyou.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=http://cm4u.co.uk.co.uk/CM4U-Contact/error.html\">";
}
?>

[/php]

Thanks for reading

This is more of the tutorial you’re looking for…

Sponsor our Newsletter | Privacy Policy | Terms of Service