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 How to get query string params in angular 12

How to get query string params in angular 12 ?

April 6, 2022October 4, 2022

In this tutorial i will show you to get the query string params in angular using ActivatedRoute. Angular completely works in different way from other javascript frameworks, Angular uses its own routing framework to land between the pages. Let’s quickly deep dive in to the topic and learn it. So…

Read More
Javascript Angular Keypress Event on Input Example

Angular Keypress Event on Input Example ?

September 21, 2022March 16, 2024

Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular Keypress Event on Input with Example. In core javascript we use onkeypress event to handle the key events of input but in angular it there is different way to handle…

Read More
Softwares disable change detection anagular

How to disable automatic change detection strategy in angular component?

July 4, 2021November 8, 2023

In angular all components by default take the changes as any event occur, but sometime we disable automatic change detection strategy in angular. Thus, to detach the two way binding in component we can use changeDetection strategy in component metadata. Angular Change Detection Strategy is simple to implement using the…

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

  • 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

  • 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
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version