Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

Click here for an alternative form with further anti-spam measures.

Click to see the contact form.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>Contact</title>
<style type="text/css">
.err {
   background-color: #cccccc;
   font-weight: bold;      
}
</style>

</head>
<body>
<h2>Contact Us</h2>

<?php
// put the emails of recipients here.
// seperate multiples by a comma a@a.co.uk, b@b.co.uk
// leave it alone for script to try and work it out.
$domain_name = eregi_replace("www.","",$SERVER_NAME);
$email_to = "contact@$domain_name";

// now this bit is a little bit clever it ASCII encodes
// the above email addresss for a link. (@ LINE 102)
// It's to help stop spambots from harvesting your email address.
function ascii_encode($str){
global $encoded;
$encoded="";
for ($i=0; $i < strlen($str); $i++){
$encoded .= '&#'.ord(substr($str,$i)).';';
}
return;
}

// this function just displays the errors, you need a
// class ".err" in the css for it to highlight properly.
// I've put a little something in above...

function err_display($err_array, $err){
   if ($err_array["$err"]){
      print ("<tr class=err>");
   }else{
      print ("<tr>");
   }
}

if (!isset($referer_url)){
   // let's try something!
   $referer_url = $HTTP_REFERER;
}

if ($submit){
   // do some testing?
      if ($first_name == ""){
         $err_array[first_name] = "<b>Error</b> please complete your first name.";
      }
   
      if ($last_name == ""){
         $err_array[last_name] = "<b>Error</b> please enter your last name.";
      }


      if ($email != "" ){
         $email = trim($email);
         if(!ereg("([_a-z0-9A-Z\d\-\.]+@[_a-z0-9A-Z\d\-]+(\.[_a-z0-9A-Z\d\-]+)+)",$email,$regs)){
            $err_array[email] = "<b>Sorry</b> your email address ($email) doesn't appear to be valid";
         }
      }else{
         $err_array[email] = "<b>Error</b> please enter your email address.";
      }

      $err_count = count($err_array);
      if ($err_count != 0){
         print ("<p class=err>Please correct the $err_count error(s):");
            while (list($index,$value) = each($err_array)){
               print ("$value<br>");
            }
         print ("</p>");
      }else{
         reset($HTTP_POST_VARS);
         while (list($key,$val) = each($HTTP_POST_VARS)){
            $message .= "$key: $val\n";
            print ("$key: $val<br>");
         }

         $email_headers = "From: $email\n";
         $email_subject = "Contact form";
         
         // this sends the email using the standard php mailer (sendmail?)
         @mail($email_to, $email_subject, $message, $email_headers);
         
         // Here's your thank you note...
         print ("<h2>Thank you $firstname!</h2>");
      }
   }


if (!$submit OR $err_count != "0"){
   ascii_encode("$email_to");
   print ("<p>Please use the form to send a message to <a href=\"mailto:$encoded\">$encoded</a></p>");
   
   
   print ("<form action=\"contact.php\" method=\"POST\">");
   print ("<table>\n<tr>");
   err_display($err_array, first_name);
   print ("<td>First name: (required)</td><td><input type=\"text\" size=14 size=14 name=\"first_name\" value=\"$first_name\"></td></tr>\n<tr>");
   
   err_display($err_array, last_name);
   print ("<td>Surname: (required)</td><td><input type=\"text\" size=14 name=\"last_name\" value=\"$last_name\"></td></tr>");
   
   
   print ("<tr>");
   err_display($err_array, email);
   print ("<td>Email: (required)</td><td><input type=\"Text\" name=\"email\" value=\"$email\"></td></tr>\n");
   print ("<tr><td>Subject: </td><td><input type=\"Text\" name=\"email_subject\" value=\"$email_subject\"></td></tr>\n");
   err_display($err_array, comments);
   print ("<td>Comments</td><td><textarea name=\"comments\" cols=\"25\" rows=\"6\">$comments</textarea></td></tr>\n");
   print ("<tr><td>How did hear about us?</td><td><input type=\"Text\" name=\"hear_about_us\" value=\"$hear_about_us\"></td></tr>\n");
   print ("<tr><td>Would you like to receive our occasional newsletter?</td><td><input type=\"Checkbox\" name=\"newsletter\"></td></tr>\n");
   print ("<tr><td>&nbsp;</td><td><input name=\"submit\" type=\"Submit\" value=\"Send\"></td></tr>\n");
   print ("</table>");
   print ("<input type=\"Hidden\" name=\"referer_url\" value=\"$referer_url\">");
   print ("</form>");
}

?>
</body>
</html>

Share this!