How to build an enquiry form using PHP.

Firstly let me explain the below function and the basics to building and enquiry form using PHP.

The function first checks if the form has been submitted, if not it displays the enquiry form.

Once the form is submitted the function checks to see if the name and email address fields are empty.
If so it echo’s out a error message asking the user to complete all required fields.
Only once these fields are not empty does it continue to check if the contents of the email field is actually an email address.
If not it echo’s out another error asking the user to fill in a valid email address.

Once the field checks are successful, it then emails the submitted form to a specified recipient.
I have also included code to redirect the user to a thank you page or simply echo a thank you message out to the user.

Depending on your requirements you could also easily modify this script to add the submitted data into a database or remove all fields except the email address and use it as a newsletter subscription form.

//Check if the form has been submitted, if not display form
if (!isset($_POST['submit'])){
<form action="#" method="post">
<label for="name">Name</label>(required)
<input type="text" name="name" id="name" value=""/>
<label for="email">Email</label>(required)
<input type="text" name="email" id="email" value=""/>
<textarea id="comment" name="comment" >Comment</textarea>
<input type="submit" name="submit" id="submit" value="submit"/>

$uname = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$subject = "Website Submission";

$headers  = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= "From:".$email."rn"."Reply-To:".$email ;

$body =	"
  <title> $subject </title>
	<style type="text/css">
	table tr td {border:1px solid #ccc;
		font-family:Verdana, Geneva, sans-serif;
	table tr {width:150px}
	<table align="center" border="1" cellpadding="0" cellspacing="0" width="500">
		<tr><td align="center" colspan="2"><h3>".$subject."</h3></td><tr>
		<tr><td class="name">Name:</td><td class="message">".$uname."</td></tr>
		<tr><td class="name">Email:</td><td class="message">".$email."</td></tr>
		<tr><td class="name" height="200">Comment:</td><td class="message">".$comment."</td></tr>
if (empty($uname)||empty($email)) {
		$msg = "<div align="center">";
		$msg.= "<p>Sorry please fill out all required fields</p>";
		$msg.= "<p><a style='color:#487300; font-size:11px' href=''>Try again.</a></p>";
		$msg.= "</div>";
    echo "<div id="msg" >".$msg."</div>";
  }else {
    if(!preg_match("/^[a-z]+[a-z0-9]*[.|-|_]?[a-z0-9]+@([a-z]+[a-z0-9]*[.|-]?[a-z]+[a-z0-9]*[a-z0-9]+){1,4}.[a-z]{2,4}$/",$email)) {
      $msg = "<div align="center">";
      $msg.= "<p>Sorry incorrect email format</p>";
      $msg.= "<p><a style='color:#487300; font-size:11px' href=''>Try again.</a></p>";
      $msg.= "</div>";
      echo "<div id="msg" >".$msg."</div>";
    }else {

//email form to recipient
      //mail ('', $subject, $body, $headers);

//redirect user to a thank you page
      //echo "<meta http-equiv='refresh' content='0;URL=http://thankyou.html'>n";
//simply echo out a thank you
	  echo "Thank you for your query, someone will be in touch with you shortly!";
    }//end if email address is not valid
  }//end if fields are empty
}//end if form submitted


Hope this helps, Nathaniel D2C.

Other Posts

%d bloggers like this: