Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Convert any form into Ajax request

Convert any form into Ajax request in jQuery

Aman Jain, November 1, 2021November 5, 2023

In this tutorial i am going to change a simple html form into Ajax form or submitting a form without reload. For this purpose i will use jQuery Ajax method and serialize method.

i also created simple jQuery plugin which you can integrate in your web application and take benefit of it. so let’s begin with some simple steps.

Step 1: Create a html form

First step is to create a html form

<div class="col-md-6 offset-md-3">
            <h3>Convert any form into Ajax form - Readerstacks.com</h3>

            <form method="post" id="validateajax" action="submit.php" name="registerform">
              <div class="form-group">
                <label>Email</label>
                <input type="text" name="email" value="" class="form-control" />

               
              </div>
              <div class="form-group">
                <label>Phone number</label>
                <input type="text" name="phone_number" value="" class="form-control" />
               
              </div>
              <div class="form-group">
                <label>Picture</label>
                <input type="file" name="picture[]"   class="form-control" />
                
              </div>
              
              <div class="form-group">
                <label>Username</label>
                <input type="text" name="username" value="" class="form-control" />
               
              </div>

              <div class="form-group">
                <button type="submit" class="btn btn-primary">Register</button>
              </div>
            </form>
          </div>

Step 2: Add jQuery and handle form submit

Next step is to include the jQuery library in page and handle the form submission.

  <script src="https://code.jquery.com/jquery-3.6.0.min.js" crossorigin="anonymous"></script>

Now, implement the jquery

$(function(){ 
    $("#validateajax").on("submit",function(e){
                e.preventDefault();
                var action = $(this).attr("action");
                var method = $(this).attr("method");
                var form_data = new FormData($(this)[0]);
                $.ajax(
                      {
                          url: action,  
                          dataType:'json', // what to expect back from the server
                          cache: false,
                          contentType: false,
                          processData: false,
                          data: form_data,
                          type: method,
                            
                          success: function (response) {
                              // display success response from the server
                          },
                          error: function (response) {
                            
                          },
                          
                      })
            });

      });

Here, we add handled submit event and then find the form action and method. In the next line, we converted the form object in FormData.

Step 3: Check the implement

Download or check live implementation

Related

Javascript HTML jQuery HTMLJavascriptjquery

Post navigation

Previous post
Next post

Related Posts

Javascript Remove hash from url in Angular

Remove hash from URL in angular 12 and deploy to server

February 1, 2022

From the first install angular come with hash URL routing and it’s defined in app-routing.module.ts. To remove hash from URL we need to change useHash key to false. Angular runs it development application ng server and whenever we wanted to deploy the application we need a server like Apache or…

Read More
Javascript How to copy object without reference in angular

How to copy object without reference in angular ?

April 13, 2022April 14, 2022

In this tutorial we will learn to copy object without reference in angular. Objects and array are work on reference by address methodology means Objects and array keeps the address means if you change anything in the object or array from anywhere then it will change the value in original…

Read More
Javascript Setup Client side javascript validation

How to Setup Client side javascript validation ?

September 16, 2021September 29, 2021

In this tutorial we will learn javascript validation without using any third party library. we will validate the simple form with 5 inputs fields. Let’s start with simple steps Step 1: Create a html file Firstly we are going to create html file which contains a form with basic fields….

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version