Submit Form Without Page Refresh Using Ajax jQuery and PHP

Submit Form Without Page Refresh Using Ajax jQuery and PHP

Submit/insert the form into MySQL database in php using jQuery ajax. In this tutorial, we will learn how to submit the form to insert data into the MySQL database without refreshing or reloading the page in PHP using jQuery Ajax.

This tutorial will show you how to submit the form and insert data to MySQL database without refreshing and reloading the web page in PHP using AJAX + jQuery.

How to Submit a Form using Ajax Without Page Refresh in PHP

Steps to submit a form using jQuery ajax without page refresh in PHP and MySQL:

  • Step 1- Create Database Connection File
  • Step 2 – Create HTML Form with Ajax Method
  • Step 3 – Create Store Data In DB File
  • Step 4 – Test This App

Step 1- Create Database Connection File

In this step, we will create a file name db.php and add the below code into your file.

	$dbname = "my_db";
		  die('Could not Connect MySql Server:' .mysql_error());

Note that :- The above code is used to create a MySQL database connection in PHP. When we insert form data into MySQL database, there you need include this file:

Step 2 – Create HTML Form with Ajax Method

In this step, we need to create an simple html form and implement jquery ajax code. So add the below code into your ajax-form.php file.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>how to submit a form without refreshing the page using jquery ajax -</title>
    <link href="" rel="stylesheet">
    <script src=""></script> 
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-offset-2">
                <div class="page-header">
                    <h2>jQuery Ajax Form Submit Example In PHP</h2>
                <p>Please fill all fields in the form</p>

                <p id="show_message" style="display: none">Form data sent. Thanks for your comments.We will update you within 24 hours. </p>

                <span id="error" style="display: none"></span>

                <form action="javascript:void(0)" method="post" id="ajax-form">

                    <div class="form-group">
                        <input type="text" name="name" id="name" class="form-control" value="" maxlength="50" >

                    <div class="form-group ">
                        <input type="email" name="email" id="email" class="form-control" value="" maxlength="30" >

                    <div class="form-group">
                        <input type="text" name="mobile" id="mobile" class="form-control" value="" maxlength="12" >

                    <input type="submit" class="btn btn-primary" name="submit" value="submit">
<script type="text/javascript">

    // hide messages 

    // on submit...



        //name required
        var name = $("input#name").val();
        if(name == ""){
            $("#error").fadeIn().text("Name required.");
            return false;

        // email required
        var email = $("input#email").val();
        if(email == ""){
            $("#error").fadeIn().text("Email required");
            return false;

        // mobile number required
        var mobile = $("input#mobile").val();
        if(mobile == ""){
            $("#error").fadeIn().text("Mobile number required");
            return false;

        // ajax
            url: "project_folder_name/ajax-form-save.php",
            data: $(this).serialize(), // get all form field value in serialize form
            success: function(){

    return false;

Step 3 – Create Store Data In DB File

Create a new file name ajax-form-save.php and update the below code into ajax-form-save.php file.

    require_once "db.php";

    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);

    if(mysqli_query($conn, "INSERT INTO customers(name, email, mobile) VALUES('" . $name . "', '" . $email . "', '" . $mobile . "')")) {
     echo '1';
    } else {
       echo "Error: " . $sql . "" . mysqli_error($conn);



The Above code is used to store form data into a MySQL database table name customers. If form successfully submitted to the database, it will return success message otherwise it returns an error.

Step 4 – Test This App

Open your browser and test this app.


Submit and insert form data into db without refreshing page in php using jquery ajax In this tutorial, we have learned how to submit and insert form data into a MySQL database without reloading or refreshing the whole web page with client-side validation.

Recommended PHP Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *