The user is required to enter in certain fields for the contact form to work correctly and send the data to an email address.
Currently, if the user doesn't fill out the required fields and clicks 'Submit' then a new .htm page appears telling them they need to go back and fill out all the required fields.
What I would like to do is give the user instant feedback on the contact form page rather than redirecting them to the new .htm page. For instance, after the user hits 'Submit' an error message appears below the submit button telling them to fill out all the required fields. How would I go about implementing this? Any help greatly appreciated!
Here's my code...
Contact Form Page:
<h4>Fields marked (*) are required</h4>
<form action="mailer.php" method="POST" >
<p>* Title: <br />
<select name="Title">
<option></option>
<option>Mr.</option>
<option>Miss.</option>
<option>Mrs.</option>
<option>Ms.</option>
<option>None</option>
</select>
</p>
<p>* First Name: <br />
<input type="text" name="FirstName"></p>
<p>* Last Name: <br />
<input type="text" name="LastName"></p>
<p>* City/Town: <br />
<input type="text" name="City"></p>
<p>* Your Email Address <br />
<input type="text" name="Email"></p>
<p>Contact Telephone Number: <br />
<input type="text" name="Tel"></p>
<p>* Message: <br />
<textarea rows="7" cols="40" name="Message"></textarea>
</p>
<br />
<!-- #-#-#-#-#- CAPTCHA Code #-#-#-#-#-#-#- -->
<p><br /> Please enter this security code into the box below:</p>
<p><img id="captcha" src="CaptchaSecurityImages.php" /></p>
<p>Security Code:
<input id="security_code" size="15" name="security_code" type="text" />
</p>
<!-- #-#-#-#-#-# END of Captcha Code #-#-#-##-## -->
<br />
<p><input type="submit" name="submit" value="Send Message"></p>
</form>
mailer.php:
<?php
// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['Email']));
$EmailTo = "kr.woods01@gmail.com";
$Subject = "Enquiry";
$Title = Trim(stripslashes($_POST['Title']));
$FirstName = Trim(stripslashes($_POST['FirstName']));
$LastName = Trim(stripslashes($_POST['LastName']));
$City = Trim(stripslashes($_POST['City']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Message = Trim(stripslashes($_POST['Message']));
// validation for text fields (i.e. first name, last name, city and tel number)
$validationFieldOK=true;
if (Trim($Title)=="") $validationFieldOK=false;
if (Trim($FirstName)=="") $validationFieldOK=false;
if (Trim($LastName)=="") $validationFieldOK=false;
if (Trim($City)=="") $validationFieldOK=false;
if (Trim($EmailFrom)=="") $validationFieldOK=false;
if (Trim($Message)=="") $validationFieldOK=false;
// #-#-#-#-#-#-# CAPTCHA Code #-#-#-#-#-#-#-#-
$validationCaptchaOK=false;
session_start();
if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
unset($_SESSION['security_code']);
$validationCaptchaOK=true;
} else {
$validationCaptchaOK=false;
}
// #-#-#-#-#-#-#- END of Captcha Code #-#-#-#-#-#-#-#
if (!$validationFieldOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=not-fill-error.htm\">";
exit;
}
if (!$validationCaptchaOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=captcha-error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Title: ";
$Body .= $Title;
$Body .= "\n";
$Body .= "\n";
$Body .= "FirstName: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "\n";
$Body .= "LastName: ";
$Body .= $LastName;
$Body .= "\n";
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= $EmailFrom;
$Body .= "\n";
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$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=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Help














