Web Design Forum: Instant Error Message Feedback on PHP Contact Form? - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Instant Error Message Feedback on PHP Contact Form? Rate Topic: -----

#1 User is offline   woodsytime 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 21-January 10
  • Reputation: 0

Posted 18 March 2010 - 10:30 AM

I've got a simple contact form written in PHP.

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\">";
}

?>

0

#2 User is offline   craftygeek 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 153
  • Joined: 17-March 10
  • Reputation: 8
  • Gender:Male
  • Location:Norfolk, UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 March 2010 - 10:38 AM

To get real time feedback on a form, you need to use Javascript...if you look at the Jquery javascript library, there are some premade form validation scripts/plugins that do this - this will probably be your easiest way forward.
0

#3 User is offline   craftygeek 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 153
  • Joined: 17-March 10
  • Reputation: 8
  • Gender:Male
  • Location:Norfolk, UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 March 2010 - 10:42 AM

have a look at these:
http://docs.jquery.c...gins/Validation
10-useful-jquery-form-validation-techniques-and-tutorials
0

#4 User is offline   woodsytime 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 21-January 10
  • Reputation: 0

Posted 18 March 2010 - 10:51 AM

Thanks for those...I'll have a look through them now!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users