Contact form with validation in PHP mailer jquery Ajax script, (PHP code to send email from contact form)

Contact form with validation in PHP mailer jquery Ajax script How to configure SMTP server to Send Email using PHP ? gmail smtp settings send email using php php code to send email from contact form phpmailer php
In this Post we will see how to create a contact form with using jquery Ajax and PHP and also insert contact form data in database table.
The PHP AJAX contact form is used to send contact information to the back end without refreshing the page. We have provided sample contact forms to collect user requests, feedback, etc. After validation by the client, data from this form is sent to the PHP page via the jQuery AJAX call. After sending the contact email, the PHP page respond to the AJAX request.
we will also insert the contact form data in database table. It’s your choice if you want to sent user feedback message into database. So, you can use the otherwise ignore this. Because In this post we will send mail using localhost with PHPMailer with SMTP.

See Also


Create database connection file 

If you want to insert contact form or feedback data in database table. So, that you need to create database connection file.
// Database configuration
$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "registration";

// Create database connection
$con = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);

// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);


Create HTML page for Contact form

This contact form contains a collection of data for user feedback. It triggers an AJAX jQuery call when the “Send Message” button is clicked. 


<!Doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Contact form with validation in PHP mailer jquery Ajax script</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>
<div class="container" style="margin-top:50px">
<h1 style="text-align:center;">Contact form with validation in PHP mailer jquery Ajax script</h1><br>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="alert alert-success alert-dismissible success" style="display: none;">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<span id="success"></span>
<div class="alert alert-danger alert-dismissible danger" style="display: none;">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<span id="danger"></span>

<form id="contactForm">
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="Name">
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="Email">
<div class="form-group">
<input type="text" class="form-control" id="subject" placeholder="Subject">
<div class="form-group">
<textarea rows="5" class="form-control" id="message" placeholder="Message"></textarea>
<div class="form-group">
<button type="submit" class="btn btn-success btn btn-block" id="btnSubmit">Send Message</button>

jQuery AJAX Contact form Mail Sending Event Handler

This jQuery script validate contact form data and, after successful verification, sends an AJAX request to the PHP page. It collects contact form data and sends them as parameters to the second page of PHP.
<script type="text/javascript">

var name = $("#name").val();
var email = $("#email").val();
var subject = $("#subject").val();
var message = $("#message").val();

if(name == "") {
$("#danger").text("Name is required");
}else if (email == "") {
$("#danger").text("Email is required");
}else if (subject == "") {
$("#danger").text("Subject is required");
}else if (message == "") {
$("#danger").text("Message is required");
url : "contacts.php",
type : "POST",
data :{name:name,email:email,subject:subject,message:message},
$("#success").text("Contact form submitted successfully");

Create PHP code for sending contact mail

In this page we will create the PHP code to send the data in database table and also send HTML email from localhost to PHP using PHPMailer. Download PHPMailer files from Github.
If you want to learn How to configure SMTP server to Send Email using PHP ?. In this post I am explain step by step.
// Include database connection file


if (isset($_POST['email'])) {

$name = strip_tags(trim($_POST[
$name = str_replace(
array("r","n"),array(" "," "), $name);
$email = filter_var(trim($_POST[
$subject = trim($_POST[
$message = trim($_POST[

$query =
"INSERT INTO contacts (name, email, subject, message)

$result = mysqli_query($con, $query);

if ($result==true) {
sendMail($email, ucwords($subject),$message);
return false;

// Create PHPMailder function for sent mail

function sendMail($to, $subject, $message){

require 'PHPMailer/PHPMailerAutoload.php';

$mail =
new PHPMailer(true);

$mail->SMTPDebug =
3; // Enable verbose debug output

// Set mailer to use SMTP
$mail->Host =
''; // Specify main and backup SMTP servers
$mail->SMTPAuth =
true; // Enable SMTP authentication
$mail->Username =
'YourEmailAddress'; // SMTP username
$mail->Password =
'YourEmailPassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port =
587; // TCP port to connect to

'Contact us');
'YourEmailAddress', 'Contact us'); // Add a recipient
$mail->addAddress('YourEmailAddress'); // Name is optional
$mail->addReplyTo($to, 'Contact us'); // Add reply to users

true); // Set email format to HTML

$mail->SMTPOptions =
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true

$mail->Subject = $subject;
$mail->Body = $message;

if($mail->send()) {
return true;
else {
return false;



You can always support by sharing on social media or recommending my blog to your friends and colleagues.   

If you have any suggestions / problems about this tutorial, please comment on the  form below.😊

Leave a Reply

Your email address will not be published.