Contact Form Improvements
Hello, I have a working contact form on what will be my works new website, but as its a first attempt it's still not quite right! and I'm not too sure how to fix it / if its possible.
the contact form is here: http://reese-test.co.uk/contact.php
1) When you click on "Send Query" without any of the fields filled in you get a pop-up as shown below (pic 1), which is nice, but if you fill in a name and then click "Send Query" it takes you to the contact_form_handler.php page and just displays the Errors in plain text as shown below (pic 2)


What I would ideally like is a pop-up for the "emailaddress" field and the "queryquestion" field and for it to not go to the contact_form_handler.php to show the other errors. Does anyone know why it is doing this?
I am using: gen_validatorv31.js for the validation which I got from a sample forms site
below is the code for the contact_form_handler.php:
<?php
$errors = '';
$myemail = 'sales@reese-test.co.uk';
if(empty($_POST['name']) ||
empty($_POST['emailaddress']) ||
empty($_POST['queryquestion']))
{
$errors .= "\n Error: all fields are required";
}
$titlename = $_POST['titlename'];
$name = $_POST['name'];
$occupation = $_POST['occupation'];
$companyname = $_POST['companyname'];
$town = $_POST['town'];
$postocode = $_POST['postcode'];
$emailaddress = $_POST['emailaddress'];
$contactnumber = $_POST['contactnumber'];
$queryquestion = $_POST['queryquestion'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$emailaddress))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Website Query: $name";
$email_body = "You have received a new query. ".
" Here are the details:\n Title: $titlename \n Name: $name \n Occupation: $occupation \n Company Name: $companyname \n Postcode: $postocode \n Email: $emailaddress \n Contact Number: $contactnumber \n Message: \n $queryquestion";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $emailaddress";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: thankyou.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
