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 Angular conditional validation in reactive forms

Angular conditional validation in reactive forms

September 26, 2022March 16, 2024

This post covers the basis of angular conditional validation in reactive forms. By the end of this post, the reader will know how to set up a simple form and validate it according to different criteria. Conditional validation is when the validity of a form control is determined by something…

Read More

How to create a new project of angular ?

August 29, 2021September 12, 2021

Before start the article i would suggest you to read our article what is angular and installation process. To create a new angular app follow the below steps: Open the terminal or command line Go to the folder in which you want to create your new angular project Example: E:/angular…

Read More
Angular How to Use Interface in Angular with Example

How to Use Interface in Angular with Example?

August 6, 2022March 16, 2024

Interface in angular or in any programming languages is pattern which bind component or class to use same implementation across the modules. In this article we will cover the use of interface in angular with example. In Angular interface can be used on service, factory, component or any type of…

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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version