The Wayback Machine - https://web.archive.org/web/20240922000507/https://www.geeksforgeeks.org/how-to-submit-a-form-using-ajax-in-jquery/
Open In App

How to submit a form using ajax in jQuery ?

Last Updated : 14 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will see the use of the AJAX request to submit a form in jQuery. The form will be submitted by sending the POST request to the server where the data will be stored.

Syntax:

$.ajax({type: value, url: value, data: value ... });

Some of the main parameters of an AJAX request are explained below:

  • type: It is used to specify the type of the request, whether the request will be of POST type or GET type.
  • url: It is used to specify the URL to which the specified type of request will be sent.
  • data: It is used to specify data to be sent to the server.

Example: The below example illustrates the use of the Ajax request to submit a form in jQuery.

HTML




<!Doctype html>
<html>
 
<head>
    <title>JQuery AJAX Call</title>
     
    <!--Include JQuery Library -->
    <script src=
    </script>
    <script>
     
    // When DOM is loaded this
    // function will get executed
    $(() => {
        // function will get executed
        // on click of submit button
        $("#submitButton").click(function(ev) {
            let form = $("#formId");
            let url = form.attr('action');
            $.ajax({
                type: "POST",
                url: url,
                data: form.serialize(),
                success: function(data) {
                     
                    // Ajax call completed successfully
                    alert("Form Submited Successfully");
                },
                error: function(data) {
                     
                    // Some error in ajax call
                    alert("some Error");
                }
            });
        });
    });
    </script>
</head>
 
<body>
    <form id='formId' action=''> Name:
        <input type='text'
               id='nm'
               name='nm'>
        <br> Student ID:
        <input type='text'
               id='studentId'
               name='studentId'>
        <br> Contact No. :
        <input type='text'
               id='contactNumber'
               name='contactNumber'>
        <br>
        <button type='submit'
               id='submitButton'>
            Submit
        </button>
    </form>
</body>
 
</html>


Output:



Similar Reads

How to submit a form without using submit button using PHP ?
In this article, we will see how to submit the form without clicking the submit button, along with understanding the different ways to accomplish this task through implementation. The form can be submitted without using submit button by implementing a specific event attribute or by clicking the link. This task can be done by using the OnClick event
3 min read
How to Disable Submit Button on Form Submit in JavaScript ?
Forms are a crucial part of web development as they allow users to submit data to a server. However, sometimes we want to customize the behavior of HTML forms and prevent the default behavior from occurring. This allows you to create a more interactive and user-friendly web application. In this article, we will discuss different approaches to preve
3 min read
How to submit a form or a part of a form without a page refresh using jQuery?
How to prevent an HTML form to submit?When submitting a form, clicking the submit button usually navigates to a new route as specified in the form's action attribute. However, this can be done in the background using event.preventDefault(), which blocks the default event behavior. By attaching an onsubmit event listener to the form, we can prevent
2 min read
Difference between Server-Side AJAX framework and Client-side AJAX framework ?
The article focuses on discussing the differences between the Server-side AJAX framework and the Client-side AJAX framework. The following topics will be discussed here: What is a Server-side AJAX framework?What is a Client-side AJAX framework?Server-side AJAX framework vs Client-side AJAX framework Let's start discussing each of these topics in de
3 min read
How to disable form submit on enter button using jQuery ?
There are two methods to submit a form, Using the "enter" key: When the user press the "enter" key from the keyboard then the form submit. This method works only when one (or more) of the elements in the concerned form have focus. Using the "mouse click": The user clicks on the "submit" form button. Approach: First, We need to select the form. This
2 min read
How to submit a form on Enter button using jQuery ?
Given an HTML form and the task is to submit the form after clicking the 'Enter' button using jQuery. To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta
2 min read
How to stop a form submit action using jQuery ?
In this article, we will learn how to stop a form submit action using jQuery. By default, the HTML form submits automatically. Submitting automatically leads to reloading the whole page again and again. Therefore for performing any operation, we have to prevent its default submission. Given a form, the task is to stop the submit action of the form
3 min read
How to disable or enable form submit button in jQuery?
Enabling or disabling the form submit button based on a checkbox selection in jQuery. This ensures user compliance with terms and conditions before form submission. Syntax:$('#enabled').click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button').removeAttr('disabled'); } else { $('#submit-button').attr('disabled', 'disabled')
2 min read
How to clear form after submit in Javascript without using reset?
Forms in JavaScript serve to gather specific user information, essential in various contexts like recruitment or inquiry submissions. While offline forms can be freely distributed, online forms must maintain a consistent structure for all users. After filling out a form, submission is crucial, but designers also tackle post-submission actions, such
2 min read
How to Submit NextJS Form to API Using FormData ?
Submitting forms is a common requirement in web applications. Next.js, a powerful React framework, simplifies the process of handling form submissions and interacting with APIs. In this article, we'll explore how to submit a form in a Next.js application using the FormData object to an API. To submit a Next.js form to an API using FormData, you fir
3 min read
How to set timeout for ajax by using jQuery?
In web programming, the Ajax is used so that the resultant data is shown in the one part of the web page, without reloading the page. The user needs to perform the Ajax request and wants the result within a timeframe. In this scenario, the jquery timeout feature is used in the code. Session timeout has been a very common feature in Ajax-based web a
4 min read
How to design navigation panel with ajax loading using jQuery EasyUI Mobile ?
In this article, we will learn how to design navigation panel with ajax loading using jQuery EasyUI Mobile plugin. EasyUI is a HTML5 framework for using user interface components based on jQuery, React, Angular and Vue technologies. It helps in building features for interactive web and mobile applications saving a lot of time for developers. Downlo
3 min read
How to pass multiple JSON Objects as data using jQuery's $.ajax() ?
The purpose of this article is to pass multiple JSON objects as data using the jQuery $ajax() method in an HTML document. Approach: Create a button in an HTML document to send JSON objects to a PHP server. In the JavaScript file, add a click event listener to the button. On clicking of the button, a request is made to PHP file using jQuery $ajax()
3 min read
How to get server response from an AJAX request using jQuery ?
In this article, we will see how we can use jQuery to get the server response to an AJAX request. The jQuery ajax() method implements the basic Ajax functionality in jQuery. It communicates with the server via asynchronous HTTP requests. Syntax:$.ajax(url);$.ajax(url,[options]);Parameters:url: A URL string to which you wish to post or get the data.
4 min read
How to create Dynamic Drag and Drop table rows using jQuery Ajax?
In this article, we will discuss the method of How to create Dynamic and Drop table rows using JQuery Ajax. We will use jQuery and HTML CSS to perform the operations of Drag and Drop on the table row. First to perform the operations of the Drag and Drop table row we need two jQuery libraries without these libraries we can't perform the Drag and Dro
4 min read
HTML | DOM Input Submit form Property
The Input Submit form Property in HTML DOM is used to return the reference of form containing the input Submit field. It is a read-only property that returns the form object on success. Syntax: submitObject.form Return Values: It returns a string value which specify the reference of the form containing the Input submit field Example: This example i
1 min read
HTML | DOM Form submit() Method
The form submit() Method in HTML DOM is used to send the form data to the web-server. It works as same as submit button. It does not contain any parameters. Syntax: formObject.submit() Example: C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;title&amp;gt; HTML DOM Form submit() Method &amp;lt;/title&amp;gt
1 min read
How to submit form on pressing Enter with Angular 9?
To submit a form in angular we have the following options: Create a button to submit the form.Assign a key to submit the formOr we can do both. In this tutorial, we will see how to use a specific key(Enter in this case) to submit a form. Approach: We can use angular keydown event to use Enter key as our submit key. Add keydown directive inside the
2 min read
JavaScript Post Request Like a Form Submit
Many web developers face the challenge of emulating the functionality of a form submission using a JavaScript POST request in situations where a form is not available or appropriate. The problem requires finding a way to replicate the behavior of a form submission using JavaScript, with the goal of accurately sending data to a server and receiving
3 min read
How to use multiple submit buttons in an HTML form ?
Every HTML form deals with the server side with an action attribute. The HTML action Attribute is used to specify where the form data is to be sent to the server after submission of the form. As the destination of our data is stored in the action attribute each and every button will lead us to the same location. To overcome this difficulty we have
2 min read
How to Create Form Submit Button in HTML ?
In HTML, the form submit button is an important element in sending the user's data to the server for further processing. We can create these buttons using two different approaches like &lt;input&gt; or &lt;button&gt; tag. Table of Content Using &lt;button&gt; tag Using &lt;input&gt; tag Using &lt;button&gt; tag In this approach, we are using the
3 min read
How to Toggle the Buttons & Submit a Form at the Same Time ?
In web development, it is common to come across situations where you need to toggle buttons and submit a form simultaneously This functionality is useful for users to interact with the form by enabling or disabling buttons while processing or submitting form data and integrating a button toggle with the form submission you can enhance the user expe
2 min read
How to submit a form by clicking a link in JavaScript ?
Submitting a form by clicking a link in JavaScript involves binding a click event handler to the link element. When clicked, the handler triggers the form's submit action programmatically, allowing form submission without the need for a traditional submit button. ApproachHTML Structure:Basic HTML structure with a title, heading, and a form to submi
2 min read
How to add Buttons without Submit the Form in Angular?
In Angular, when adding buttons to a form, their default behavior on click is to submit the form itself. However, there are certain situations in which it is necessary to create buttons for forms that provide some sort of functionality or trigger custom logic without triggering the default form submission behavior. Steps to Configure the Angular Ap
5 min read
How To Reset a React-Bootstrap Form After Submit?
Forms are one of the most useful elements in web applications. It allows users to input their credentials and submit those data. There are many types of forms present (User Registration, Login, Feedback Form etc). In this article, we will see how to reset a React-Bootstrap Form after submission. PrerequisitesJavaScriptReact-BootstrapReact BasicsRea
3 min read
jQuery submit() Method
The jQuery submit() method is used to submit events or attaches a function to run when a submit event occurs. This method can only apply to the form elements. Syntax: $(selector).submit(parameters);Parameter: The method is an optional parameter. Return Value: This method returns the selected element along with the attached event. Example 1: In this
2 min read
jQuery :submit Selector
The jQuery :submit Selector is an inbuilt selector which is used to select the submit button and input element having submitted type field. If the type of button is undefined, most of the standard browsers use it as type="submit". Syntax: $(":submit")Example 1: In this example, we will select the input element which has a submit field button by usi
2 min read
AJAX full form
AJAX is an abbreviation for Asynchronous JavaScript and XML. AJAX is a technique not a programming language which is used by the developers to make the websites behaving like desktop applications. It operates on the client-side for creating asynchronous web applications. AJAX is a group of technologies that uses number of web technologies for creat
4 min read
How ajax works? Difference between angular js and jquery
Ajax: Ajax communicates with the server by using XMLHttpRequest Object. User send request from User Interface and JavaScript call goes to the XMLHttpRequest Object after that XMLHttp request is sent to the XMLHttpRequest Object. At that time server interacts with the database using php, servlet, ASP.net etc. The data is retrieved then the server se
2 min read
jQuery AJAX Complete Reference
AJAX is a technique, not a programming language that is used by developers to make websites behave like desktop applications. It operates on the client-side for creating asynchronous web applications. AJAX is a group of technologies that uses a number of web technologies for creating a set of web development techniques. jQuery provides various meth
2 min read