Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nikhil_lu210's avatar

How to add Google ReCaptcha in contact form using Ajax & PHP?

I want to add google recaptcha into my web contact validation form. What should I do...?

Form:

<form id="contact">
    <div class="row">
        <div class="col-md-12">
            <div id="msg"></div>
        </div>
        <div class="col-sm-6 col-xs-12">
            <div class="input-field-wrap">
                <input type="text" class="input-field" placeholder="First Name *" name="first_name" required>
                <div class="focus"></div>
            </div>
            <div class="empty-sm-20 empty-xs-20"></div>
        </div>
        <div class="col-sm-6 col-xs-12">
            <div class="input-field-wrap">
                <input type="text" class="input-field" placeholder="Last Name" name="last_name">
                <div class="focus"></div>
            </div>
            <div class="empty-sm-20 empty-xs-20"></div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6 col-xs-12">
            <div class="input-field-wrap">
                <input type="email" class="input-field" placeholder="Email *" name="email" required>
                <div class="focus"></div>
            </div>
            <div class="empty-sm-20 empty-xs-20"></div>
        </div>
        <div class="col-sm-6 col-xs-12">
            <div class="input-field-wrap">
                <input type="text" class="input-field" placeholder="Subject" name="subject">
                <div class="focus"></div>
            </div>
            <div class="empty-sm-20 empty-xs-20"></div>
        </div>
    </div>
    <div class="input-field-wrap">
        <textarea placeholder="Message *" class="input-field" name="message" required></textarea>
        <div class="focus"></div>
    </div>
    <div class="empty-sm-30 empty-xs-30"></div>
    <div class="text-center">
        <div class="page-button button-style-1 type-2">
            <input type="submit" id="submit-btn">
            <span class="txt">SEND MESSAGE</span><i></i>
        </div>
    </div>
</form>

Ajax Request

$('#submit-btn').click(function(event) {
    $.ajax({
        dataType: 'JSON',
        url: 'mail-process.php',
        type: 'POST',
        data: $('#contact').serialize(),
        beforeSend: function() {
            $('.txt').html('SENDING...');
        },
        complete: function() {
            $('.txt').html('SEND MESSAGE');
        },
        error: function() {
            $('#msg').html('<div class="alert alert-danger">Please Fillup Properly, and Try Again.</div>').hide();
        }
    });
    $('#msg').html('<div class="alert alert-success">Message successfuly Sent.</div>').hide().fadeIn(1500);
    $('#msg').addClass('d-none');
    $('#contact')[0].reset();
    return false;
});

PHP

<?php
require_once 'mailer/class.phpmailer.php';
require_once 'database/db-connection.php';


$first_name = $_POST['first_name'];
$last_name  = $_POST['last_name'];
$email      = $_POST['email'];
$subject    = $_POST['subject'];
$message    = $_POST['message'];

$name = $first_name. ' ' . $last_name;


/*=======================================================
=< Contact Controller for Inserting Data into Database >=
=========================================================*/
mysqli_query($connection,"insert into contacts (name, email, subject, message) values ('$name', '$email', '$subject', '$message')") or die(mysqli_error());



$message ='
Name:            '.$name. '<br />
Email:           '.$email.'<br />
Subject:         '.$subject.'<br />
Message:         '.$message. '<br />
';


// creates object
$mail = new PHPMailer(true);
$reply_mail = new PHPMailer(true);
$reply_mail_id = $email; //Client email
$subject = "Contact Mail"; //client subject 

$reply_message = "Dear Customer, Thank you for your message. We will contact you soon"; //client message

$mail->MsgHTML($message);
$reply_mail->MsgHTML($reply_message);


try
{
$mail->IsSMTP();
$mail->isHTML(true);
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = '465';
$mail->Username ="[email protected]";
$mail->Password ="pass";
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->Body = $message;
$mail->AltBody = $message;


//Reply to client
$reply_mail->IsSMTP();
$reply_mail->isHTML(true);
$reply_mail->SMTPDebug = 0;
$reply_mail->SMTPAuth = true;
$reply_mail->SMTPSecure = "ssl";
$reply_mail->Host = "smtp.gmail.com";
$reply_mail->Port = '465';
$reply_mail->AddAddress($reply_mail_id); // Client get success email
$reply_mail->Username ="[email protected]";
$reply_mail->Password ="pass";
$reply_mail->SetFrom("[email protected]", "Restaurant");
$reply_mail->AddReplyTo($_POST['email']);
$reply_mail->Subject = "New Reservation";
$reply_mail->MsgHTML($reply_message);
$reply_mail->Body = $reply_message;
$reply_mail->AltBody = $reply_message;



    // Send To
    $mail->AddAddress("[email protected]", "Restaurant"); // Where to send it - Recipient
    $result = $mail->Send();
    $reply_result = $reply_mail->send();
if($reply_mail->Send())
{
    $previous = "javascript:history.go(-1)";
    echo "<script>
    alert('Thanks a lot for connect with us. We Will Contact you very soon.');
    window.location.href= '$previous';
    </script>";
}

}
catch(phpmailerException $ex)
{
$msg = "
".$ex->errorMessage()."
";
}
?>
0 likes
1 reply

Please or to participate in this conversation.