Jquery Ajax Post Example for Submitting AJAX Forms in PHP

Lately, the AJAX forms seem to be used everywhere. In fact, as a custom PHP development company most of the PHP web app projects we develop are using AJAX forms. And, if you look at any Jquery Ajax Post example, you’ll notice that the code looks easier, shorter, and more readable.

For example, tasks such as creating a catch for different browser XMLHttpRequest, opening any URL, checking if the request is ready or not have the short and more readable syntax in Jquery Ajax.

Today, in our PHP tutorial, we’ll be shown the implementation process for submitting AJAX forms in PHP with Jquery Ajax Post Example or you can say Jquery submit form Ajax.

In this Ajax example demo, we’ll use serialize() method for creating URL encoded text string by serializing form values. These serialized values can be used in the URL query string whenever you’re making an AJAX request.

Before getting started, share it with your friends with just a single click!

Now Let’s Get Started

hand icon Create a file with Index.php to display the feed in the browser.

hand icon Now, define a header for the page.

<html>
    <head>
        <title>Space-O | Ajax submit form</title>
        <style type="text/css">
        table {
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
        }
        </style>
    </head>
    <body>
        <h1>jQuery post form data using .ajax() method</h1>
Copy to Clipboard

hand icon Next, create a form for obtaining details from the end-user.

<form id='userForm'>
    <div>
        <input type='text' name='name' placeholder='Name' />
    </div>
    <div>
        <input type='text' name='email' placeholder='Email' />
    </div>
    <div>
        <input type='text' name='number' placeholder='Mobile Number' />
    </div>
    <div>
        <input type='submit' value='Submit' />
    </div>
</form>
Copy to Clipboard

hand icon Create a Div to display information that is entered by end-user and success message.

<div id='response'></div>
Copy to Clipboard

hand icon Now, add Javascript to use Jquery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
Copy to Clipboard

hand icon In below code, the entire logic, as well as standard of code, is defined.

<script>
$(document).ready(function() {
    $('#userForm').submit(function() {
    // show that something is loading
    $('#response').html("Loading response...");

    // Call ajax for pass data to other place
    $.ajax({
        type: 'POST',
        url: 'get_information.php',
        data: $(this).serialize() // getting filed value in serialize form
    })
    .done(function(data) { // if getting done then call.
        // show the response
        $('#response').html(data);
    })
    .fail(function() { // if fail then getting message
        // just in case posting your form failed
        alert("Posting failed.");
    });

    // to prevent refreshing the whole page page
    return false
    });
});
</script>
Copy to Clipboard

Want to Create a Web Application?

Need to validate your app idea or consult with an expert? Get a free consultation now!

Cta Image

Full Code of Jquery AJAX Post Example:

<html>
<head>
    <title>Space-O | Ajax submit form</title>
</head>
<body>
    <h1>jQuery post form data using .ajax() method</h1>
    <!-- our form -->
    <form id='userForm'>
    <div>
        <input type='text' name='name' placeholder='Name' />
    </div>
    <div>
        <input type='text' name='email' placeholder='Email' />
    </div>
    <div>
        <input type='text' name='number' placeholder='Mobile Number' />
    </div>
    <div>
        <input type='submit' value='Submit' />
    </div>
    </form>
    <style type="text/css">
    table {
        border-collapse: collapse;
    }
    
    table, th, td {
        : 1px solid black;
    }
    </style>
    <!-- where the response will be displayed -->
    <div id='response'></div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $('#userForm').submit(function(){

        // show that something is loading
        $('#response').html("<b>Loading response...</b>");

        // Call ajax for pass data to other place
        $.ajax({
            type: 'POST',
            url: 'get_information.php',
            data: $(this).serialize() // getting filed value in serialize form
        })
        .done(function(data){ // if getting done then call.

            // show the response
            $('#response').html(data);

        })
        .fail(function() { // if fail then getting message

            // just in case posting your form failed
            alert( "Posting failed." );

        });

        // to prevent refreshing the whole page page
        return false;

        });
    });
    </script>
</body>
</html>
Copy to Clipboard

And done!

jquery ajax post example

Submitting a JQuery AJAX form is a fairly easy process. However, you’ll want to look at doing client-side verification as well to make sure the job is done correctly. And, there are plenty of resources to push forward. You could either hire PHP programmers or reach out to experts if you’re starting out to learn web app development programming.

And if you’re already familiar with the fundamentals of jQuery and AJAX in PHP, you might be interested in learning how to use these technologies to create a dynamic event calendar on your website. Our post on how to build an event calendar with PHP using JQuery and AJAX, is a great resource for you. By adding this feature into your website, you can give your users a more interactive and engaging experience.

In case, if you still have any query or confusion regarding submitting a jQuery AJAX form, jQuery Input value, then you can get in touch with us through our contact us form. One of our sales representatives will revert to you as soon as possible. The consultation is completely free of cost.

Bhaval Patel

Written by

Bhaval Patel is a Director (Operations) at Space-O Technologies. He has 20+ years of experience helping startups and enterprises with custom software solutions to drive maximum results. Under his leadership, Space-O has won the 8th GESIA annual award for being the best mobile app development company. So far, he has validated more than 300 app ideas and successfully delivered 100 custom solutions using the technologies, such as Swift, Kotlin, React Native, Flutter, PHP, RoR, IoT, AI, NFC, AR/VR, Blockchain, NFT, and more.