Serialize only Selected Elements of a Form and Submit it using jQuery
Posted by: Suprotim Agarwal ,
on 1/11/2010,
in
Category jQuery and ASP.NET
Abstract: This article will demonstrate how to submit form data in the background without using ASP.NET AJAX and at the same time prevent some form fields from being submitted.
Serialize only Selected Elements of a Form and Submit it using jQuery
This article will demonstrate how to submit form data in the background without using ASP.NET AJAX and at the same time prevent some form fields from being submitted.
Note that for demonstration purposes, I have included jQuery and CSS code in the same page. Ideally, these resources should be created in separate folders for maintainability.
Let us quickly jump to the solution and see how to submit form data in the background using jQuery
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Serialize Form Data</title>
<script type='text/javascript'
src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'>
</script>
<script type="text/javascript">
$(function() {
$('input[id$=btnSubmit]').click(function(e) {
e.preventDefault();
var params = $('form[id$=form1]').serializeArray();
$.post("R51a-ReadValues.aspx",
$(params).filter(function() {
return !({ "txtLName": 1, "__VIEWSTATE": 1, "__EVENTVALIDATION": 1 })[this.name];
}),
function(data) {
//do something
},
"json"
);
alert('Form data submitted in the background');
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="tableDiv">
<h2>Enter data and click on 'Serialize and Submit' button
to send form data to a different page without a postback</h2>
<br /><br />
<asp:Label ID="lblFName" runat="server"
Text="FirstName: "></asp:Label>
<asp:TextBox ID="txtFName" runat="server">
</asp:TextBox><br />
<asp:Label ID="lblMName" runat="server"
Text="MidName: "></asp:Label>
<asp:TextBox ID="txtMName" runat="server">
</asp:TextBox><br />
<asp:Label ID="lblLName" runat="server"
Text="LastName: "></asp:Label>
<asp:TextBox ID="txtLName" runat="server">
</asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server"
Text="Serialize and Submit" />
</div>
</form>
</body>
</html>
$.post() is a very useful method for sending a simple POST request to the server using AJAX. The signature of $.post() is as follows:
$.post( url, [data], [callback], [type] )
where url is the URL where the request is sent, [data] is the data sent with the request, [callback] is a function to execute when the request is complete and [type] is the format in which content has been returned (xml, json)
So a simple piece of code like the one shown below can send the form data to another page using AJAX.
$.post("R51a-ReadValues.aspx", $('form[id$=form1]').serialize());
Now what if you want to prevent some fields from being submitted to the server? So apart from the form fields, there is the ViewState and EventValidation that also gets posted to the server. Let us save some bandwidth by exploring how to prevent these fields as well as some other Form fields too for demonstration purposes (like the lastname) from being posted to the server. Observe this piece of code:
var params = $('form[id$=form1]').serializeArray();
$.post("R51a-ReadValues.aspx",
$(params).filter(function() {
return !({ "txtLName": 1, "__VIEWSTATE": 1, "__EVENTVALIDATION": 1 })[this.name];
}),
function(data) {
//do something
},
"json"
);
In the code above, we use serializeArray() to serialize all forms and form elements. A JSON structure is returned which is stored in the variable ‘params’. When you use $.post() to submit the form fields, we use the filter() function to filter out the txtLName, __VIEWSTATE and __EVENTVALIDATION fields. Imagine the performance increase when we are able to reduce the data posted. Now when you run the application, enter data in the text fields and submit data, you will see that only two fields are posted – txtFName and txtNName.
Note: Since the form is being posted in the background, you will not see the postback happening. However the output can be seen using FireBug as shown below.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn