PHP Contact Form with jQuery AJAX

Last modified on June 22nd, 2019 by Vincy.

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

If you are looking for a AJAX based, responsive, secured contact form component in PHP, get the best contact form you will ever find in the Internet. Have a look at its live preview and play-around to know more about it.

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

View DemoDownload

jQuery AJAX Contact Form

This form contains the 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 request to a PHP page on successful validation. It collects contact form input and sends it as the parameters to the second PHP page.

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 have seen this code when we saw about 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

↑ Back to Top

Share this Article