PHP Contact Form with jQuery AJAX

by Vincy. Last modified on July 6th, 2023.

PHP AJAX contact form sends contact information to the back end without page refresh. The AJAX-based contact form will be helpful when this component is widgetized in an application layout.

In this tutorial, we gave an example contact form for collecting user queries, feedback, etc. After client-side validation, these form data will be sent to a PHP page through a jQuery AJAX call. After sending the contact email, the PHP page will respond to the AJAX request.

View Demo


jQuery AJAX Contact Form

This form contains a collection of inputs to get user feedback. It triggers jQuery AJAX call on the click event of its submit button.

<div id="frmContact">
    <div id="mail-status"></div>
        <label style="padding-top:20px;">Name</label><span id="userName-info" class="info"></span><br/>
        <input type="text" name="userName" id="userName" class="demoInputBox">
        <label>Email</label><span id="userEmail-info" class="info"></span><br/>
        <input type="text" name="userEmail" id="userEmail" class="demoInputBox">
        <label>Subject</label><span id="subject-info" class="info"></span><br/>
        <input type="text" name="subject" id="subject" class="demoInputBox">
        <label>Content</label><span id="content-info" class="info"></span><br/>
        <textarea name="content" id="content" class="demoInputBox" cols="60" rows="6"></textarea>
        <button name="submit" class="btnAction" onClick="sendContact();">Send</button>

jQuery AJAX Contact Mail Sending Event Handler

This jQuery script validates contact form input and sends AJAX requests to a PHP page on successful validation. It collects contact form input and sends it to the second PHP page as the parameters.

function sendContact() {
    var valid;	
    valid = validateContact();
    if(valid) {
            url: "contact_mail.php",
            type: "POST",
            error:function (){}

The client-side validation script is,

function validateContact() {
    var valid = true;	
    if(!$("#userName").val()) {
        valid = false;
    if(!$("#userEmail").val()) {
        valid = false;
    if(!$("#userEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
        valid = false;
    if(!$("#subject").val()) {
        valid = false;
    if(!$("#content").val()) {
        valid = false;
    return valid;

PHP Code for Sending Contact Mail

We saw this code in the PHP contact form tutorial without AJAX or jQuery.

    $toEmail = "";
    $mailHeaders = "From: " . $_POST["userName"] . "<". $_POST["userEmail"] .">\r\n";
    if(mail($toEmail, $_POST["subject"], $_POST["content"], $mailHeaders)) {
        print "<p class='success'>Mail Sent.</p>";
    } else {
        print "<p class='Error'>Problem in Sending Mail.</p>";

View DemoDownload

Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

↑ Back to Top

Share this page