Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

3 Steps Simple PHP Contact Form With Recaptcha

 INTRODUCTION
CONTACT FORM WITH ANTI-SPAM

Welcome to a step-by-step tutorial on how to create a simple PHP contact form with reCAPTCHA. Is your website under attack from nasty spammers? Fear not, we will walk through how to create a contact form and send an email upon submission – Only after the user has passed a reCAPTCHA test and identify themselves as human. As an added extra, we will also go into how to deal with the captcha in AJAX forms. Read on to find out!

I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!

NAVIGATION
TABLE OF CONTENTS

Step 1
Register & Get Keys

Step 2
HTML Form

Step 3
Verify Captcha

Extra
AJAX Forms

Extra
Download

Closing
What Next?

STEP 1
REGISTER WITH GOOGLE & GET THE KEYS

For the captcha, we will be using a free service by good old Google – Google reCAPTCHA. So the first step is to sign up and register your website.

REGISTRATION PROCESS

Go to the Google Recaptcha Admin Panel, and register your website. Please do take note that if you want to test the captcha on your local machine, you have to add localhost and/or 127.0.0.1 into the domains list.


When you are done with the registration, Google will throw you a bunch of keys and code fragments. We will be using these later, and don’t worry, they are actually very straightforward. You don’t have to copy them down on a piece of paper for now, you can always check back in the admin panel to get all the keys. 

EXTRA) SECURITY STRENGTH

Extra tip – If you are still getting a lot of spambot messages after implementing the captcha, you can change the security level. Go back to the Google Recaptcha Admin panel, turn the slider all the way to “most secure” under the advanced settings. Some users may get a more difficult captcha challenge, but it does filter out a lot of the unwanted spam.

STEP 2
HTML FORM

Next step, we will create the HTML form, do some CSS cosmetics, and put in the captcha code.

THE CONTACT FORM 

form.html


  
    

Contact Us

The client-side HTML contact form is pretty much just a regular Joe form. But to implement the reCAPTCHA, you need to do 2 things:

  • You need to insert the Javascript (that Google has provided) into the head section.
  • Place the reCAPTCHA div wherever in the form that you want to display the captcha.

That’s it, you now have a fully functioning form with captcha.

THE CSS

form.css
html, body, input, textarea{
  font-family: arial;
}
#contact{ 
  background: #eee; 
  padding: 15px;
}
#contact label{
  display: block;
  margin-top: 10px;
  font-weight: bold;
}
#contact input, #contact textarea{
  width: 100%;
  box-sizing: border-box;
}
#contact input[type=text], #contact input[type=email], #contact textarea{
  padding: 10px;
}
#contact input[type=submit]{
  background: #b9d1f7;
  border: 0;
  margin-top: 10px;
  padding: 10px;
}

Nothing much, just some cosmetics. Feel free to change these to fit your own website.

STEP 3
VERIFY CAPTCHA

As for the final step, we only need to verify the captcha on the server-side, then process accordingly – Send out an email, save to a database, or whatever you want.

THE SCRIPT

submit.php
success) {
  $to = "[email protected]"; // CHNAGE THIS TO YOUR OWN!
  $subject = "Contact Form";
  $message = "Name - " . $_POST['name'] . "
"; $message .= "Email - " . $_POST['email'] . "
"; $message .= "Message - " . $_POST['message'] . "
"; if (@mail($to, $subject, $message)) { // Send mail OK - Show a nice thank you page or something } else { // Send mail error... Ask user to retry or give alternative } } else { // Invalid captcha - Ask user to retry } ?>

THE EXPLANATION

This script should be pretty straightforward and easy to understand:

  • There are 2 parts to it, the top part will send the POSTED captcha code to Google for verification – Just remember to change the secret key to your own (obtained in step 1).
  • After the verification is complete, the bottom half will formulate and send out the contact email.
  • Again, this script is only barebones. Please change the “email to”, format the email nicely, and handle the errors properly – Maybe redirect the user back to the contact form and show the error messages.

EXTRA
AJAX FORMS

That’s it for the contact form and captcha. But are you submitting the form via AJAX instead? No problem, here is how to deal with the reCAPTCHA in AJAX.

THE AJAX CONTACT FORM

form-ajax.html


  
    

Contact Us

form-ajax.js
function ajaxsubmit() {
  $.ajax({
    url : "submit.php",
    method : "POST",
    data : {
      name : $('#name').val(),
      email : $('#email').val(),
      message : $('#message').val(),
      'g-recaptcha-response' : grecaptcha.getResponse()
    }
  }).done(function(res){
    // DO YOUR PROCESSING HERE
    console.log(res);
  });
  return false;
}

THE EXPLANATION

Actually, there is not much of a difference when you want to use AJAX to submit the form. Just create a new Javascript function to get the form values, then post it to the server side for processing; We can use the grecaptcha.getResponse() function in the Google API to get the captcha response and post it to our server-side script as usual.

EXTRA
DOWNLOAD

Finally, here is the download link for all the scripts in this tutorial as promised.

QUICK START

Skipped the entire tutorial? There is no database required for this example, so just download and unzip into your project folder. Access form.html and just trace how it works from there – If you are planning to use AJAX, trace from form-ajax.html instead.

USEFUL LINKS

  • Google reCAPTCHA home page
  • The official Google reCAPTCHA developer’s guide

SOURCE CODE DOWNLOAD

Click here to download the source code in a zip file – I have released it under the MIT License, so feel free to build on top of it if you want to.

CLOSING
WHAT NEXT?

Thank you for reading, and we have come to the end of this short tutorial. I hope it will help you to fight off some nasty spam on your website, and if you have stuff you wish to add on to this guide, please feel free to comment below. Good luck and happy coding!

The post 3 Steps Simple PHP Contact Form With Recaptcha appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

3 Steps Simple PHP Contact Form With Recaptcha

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×