Create a Simple PHP Contact Form





Creating a contact form in standard HTML isn’t very safe with regards to keeping Spammers away – they’ll see your email address in the HTML and now you’re all setup to receive Spam emails from all over the world!

PHP contact forms are a much safer option because the email address is embedded in the PHP code, and PHP code isn’t visible when a web page is rendered – therefore no one can see the email address you have specified!

The PHP code here is specifically for anyone who wants their contact form to go to two email addresses.

Here is the PHP code for the contact form:


<?php
 //Recieve variables
$submit = $_POST[submit];
$name = $_POST[name];
$email_address = $_POST[email_address];
$tel = $_POST[tel];
$enquiry = $_POST[enquiry];
$main_email = "email1@email.com";
$secondary_email = "email2@email.com";

// The Validation for the Form

if(isset($submit)){
 if((!strcmp($name, '')) || (!strcmp($email_address, '')) || (!strcmp($enquiry, ''))){
 $warning = true;
 $message = "Please fill in required fields.";
 }

// Process form
if(!isset($warning)){
 $email_subject = "SUBJECT OF YOUR EMAIL";
 $email_message = "The following form has just been submitted at the YOUR WEBSITE:\r\n\r\n";
 $email_message .= "
 Name: $name\r\n\r\n
 Email: $email_address\r\n\r\n
 Tel: $tel\r\n\r\n
 Enquiry: $enquiry\r\n\r\n
 ";

if(mail($main_email, $email_subject, $email_message, "From: YOURMAIL@EMAIL.COM\r\n")){
  mail($secondary_email, $email_subject, $email_message, "From: YOURMAIL@EMAIL.COM\r\n");
  $sent = true;
  }else{
  $sent = false;
  }
 }
}
?>

The HTML Code for the contact form, although very basic (it’s up to you to make it fancy! ;) ) – goes like this:

<?php
if(isset($sent) && $sent == true)
{
echo "Thankyou $name for contacting us. We will get back to you about your enquiry as soon as possible.";
}else{
?>

<?php if(isset($warning)){echo "$message";} ?>
<form method="post" action="contact.php" name="contact">

Name: <input name="name" type="text" value="<?php echo $name; ?>"/>

Email: <input name="email_address" type="text" value="<?php echo $email_address; ?>"/>

Tel: <input name="tel" type="text" value="<?php echo $tel; ?>"/>

Enquiry: <textarea name="enquiry"><?php echo $enquiry; ?></textarea>

<input name="submit" type="submit" value="CONTINUE" />

</form>
<?php } ?>

The form action is the page it goes to once the form has been submitted. I called the page “contact.php”, and the action is “contact.php”, which basically means it reloads the same page but with a “Thank you” message in place of the contact form.

Notice the variables at the top (name, email, tel) etc. they are given the same name as the textfield it responds to. Do not change these as it may cause the contact form to fail.

If you enjoyed this post, make sure you subscribe to my RSS feed!


bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top


17 Comments »

  1. [...] than using HTML contact forms as your email address isn’t visible to world-wide spammers!read more | digg story Share and Enjoy: These icons link to social bookmarking sites where readers can [...]

    comment-bottom
  2. Danyael Says:

    Dear Webmaster, Where do I put the files on the server? They can see the php file contact.php and contact.html duh? If they can see the contact.php then they also see the mail address.

    Best regards,
    Danyael

    comment-bottom
  3. Emz Says:

    Hi Danyael,

    Thanks for your question.

    You upload the contact.php to your server as you would any file. There isn’t any need for a contact.html file though – it’s all PHP.

    Now, if you view the page in your browser and click view source, you’ll see all the PHP is hidden. Only HTML is rendered in a PHP file. The PHP still works but it doesn’t show when it’s rendered in HTML.

    Therefore, gaining access to the email address is impossible because the email address is specified within the PHP, therefore it’s hidden.

    comment-bottom
  4. sturat Says:

    Hello, Thanks for sharing this information. I am learning PHP and found your blog with some informative posts. I have bookmarked it.

    Thanks

    comment-bottom
  5. Muzammal Says:

    Thanks for sharing some useful information here. I’m still learning PHP programming and I think I will find some more tips here

    comment-bottom
  6. Danyael Says:

    Hi Emz, Thank you for the response. The form works great.

    Best regards,

    Danyael

    comment-bottom
  7. Danyael Says:

    Hi Emz, I have been working with your form and it’s really great, but now I am in a phase to put some extra things in it. Can you help me out with that?

    - I would like to have a select option
    - ticker box
    - radio button
    - some validation

    Is that also possible with your form?

    Best regards,

    Danyael

    comment-bottom
  8. Danyael Says:

    Dear Emz, I have copied your code again, but now it seems to giving me an error

    Parse error: syntax error, unexpected T_STRING in

    Best regards, Danyael

    comment-bottom
  9. Great code, works perfectly, thanks.

    comment-bottom
  10. Rob Says:

    Hi Danyael, I’m getting the same thing you are with that error. Does anyone know what that means:

    “Syntax Error, Unexpected T_STRING”

    As soon As I hit “continue” I get that error.

    comment-bottom
  11. James Says:

    Dear EMZ,

    thanks for making this script available to learners (such as me!) i am experiencing a difficulty the same as “danyael” has in his post of september 21st 2008:

    Parse error: syntax error, unexpected T_STRING in D:\websites\MQ CompareMortgage.co.uk\contact.php on line 25

    line 25 of the code is (from what i can see and i dont mean to tell you what your code is!):

    $message = “please fill in required fields.”;

    any help would be appreciated

    comment-bottom
  12. Thanks For the Code, Great article.

    comment-bottom
  13. I also want to thankyou for sharing this information. This is great tutorial since I am PHP beginner.

    comment-bottom
  14. Any chance of some guidence in regard to implementing this on a wordpress blog?

    comment-bottom
  15. Gify Says:

    Hi Emma,
    Thanks for the script will try it on my website :)
    Regards,
    Janet Gify

    comment-bottom
  16. Thank you for the helpful code. I’d love to learn how to PHP coding! But yeah, thanks.

    comment-bottom

RSS feed for comments on this post. TrackBack URL

Leave a comment