Jquery ajax form submit example

Abstract: Submit a khung programmatically using jQuery Ajax. We will also filter empty size fields From submitting.
There are two different ways to submit a size programmatically. You can either use jQuery Ajax lớn submit the khung asynchronously and use the response inside the current page OR you can use the normal form submission mechanism, which will load the response in a new browser window.

Bạn đang xem: Jquery ajax form submit example

Most jQuery Ajax methods take care of serializing user input automatically in the background. However when you are manually sending data via Ajax, you will have sầu to lớn use the jQuery serialize() method. This method acts on a jQuery object and translates the matched DOM elements inkhổng lồ a text string in standard URL-encoded notation. This string can then be passed along with an Ajax request.

The jQuery documentation mentions that only "successful controls" are serialized to lớn the string. This excludes those elements that are disabled or radio buttons that have the same "name" attribute or controls that don’t have sầu a name attribute. The Values of đầu vào type "radio" or "checkbox" are included only if they are checked. Data from Select element is not serialized unless specified in the serialize() method.

Let"s see how khổng lồ submit a size programmatically using jQuery Ajax. Create a new tệp tin ‘SubmitFormUsingAjax.html’. Write the following code:

$("#simpleform").on("submit", function (event) var $this = $(this); var frmValues = $this.serialize(); $.ajax( type: $this.attr("method"), url: $this.attr("action"), data: frmValues ) .done(function () $("#para").text("Serialized! Input String is " + frmValues); ) .fail(function () $("#para").text("An error occured"); ); sự kiện.preventDefault(); );Let’s understvà the code.

$("#simpleform").on("submit", function (event) );While submitting forms, it’s a good practice (for future code compatibility) lớn bind to lớn the form submit event rather than a button clichồng event.

var $this = $(this);var frmValues = $this.serialize();We are storing the form object in a variable so that it can be used later. The khung data is placed inlớn a variable by using the jQuery serialize() method. This method forms a key-value query string by taking all of the khung đầu vào names, as well as their values. This string can then be parsed by the server-side script for further processing.

The final bits of code is to lớn use $.ajax() lớn submit the khung lớn the server.

$.ajax( type: $this.attr("method"), url: $this.attr("action"), data: frmValues).done(function () $("#para").text("Serialized! Input String is " + frmValues);).fail(function () $("#para").text("An error occured"););Here we are using the attributes like post method, url defined on the size element. So tomorrow if we decide lớn change it, we vị not have sầu lớn touch the JavaScript code as it will pichồng the update attributes automatically. In the done() function, for the sake of it, we are posting the serialized data in a paragraph.

Run the sample & enter some details as shown here:

*

Cliông chồng the Submit button and the serialized data will be passed to lớn the VPS. Since the form is being posted in the background, you will not see the postbachồng happening. However the output can be seen using FireBug as shown below.

Xem thêm: 7 Bước Tải Game Ve Iphone - ‎Among Us! On The App Store

*

Live Demo

Filter Empty Form fields From Submitting

By default, all form fields that are successful controls get serialized. What if you wanted khổng lồ prevent some fields from submitting? The answer lies in the serializeArray() method. The serializeArray() method creates a JavaScript array of objects, ready khổng lồ be encoded as a JSON string. In order to eliminate some fields, you have sầu to lớn iterate these objects và filter them as appropriate.

In this example, we will filter all those text fields which vì not have sầu any value in them. Create a new file ‘FilterFormFields.html’. The code remains exactly the same as we saw in our previous example. Only the following changes are needed:

$("#simpleform").on("submit", function (event) ... var frmValues = $this.serializeArray() .filter(function (elem) return $.trim(elem.value) != ""; ); ...);As you can see, we are using filter() khổng lồ reduce the mix of matched elements khổng lồ those elements that have some value in it.

Run the sample, enter a value in only the First Name field and hit Submit

*

The output can be seen using FireBug as shown below:

*

As you can see, only the fields which had some value were serialized and submitted.

serialize() vs serializeArray()?

As we saw, serializeArray() creates an array object, whereas serialize() creates a string (query string). serialize() is a convenient approach, but if you want a data structure that you want lớn operate on as we just saw, serializeArray() is the way to go.

Live Demo

If you liked this article, take a look at my new book The Absolutely Awesome jQuery Cookbookwhich contains scores of practical jQuery/jQueryUI recipes you can use in your projects right away.

This article has been editorially reviewed by Suprotlặng Agarwal.


*

C# & .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at obatambeienwasirherbal.com are very excited khổng lồ announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), và Mobi (Kindle).

Organized around concepts, this Book aims to lớn provide a concise, yet solid foundation in C# & .NET, covering C# 6.0, C# 7.0 và .NET Core, with chapters on the lachạy thử .NET Vi xử lý Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to lớn deepen your existing knowledge of C# and .NET, to have a solid grasp of the lademo in C# and .NET OR khổng lồ crachồng your next .NET Interview.