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.
Give me a +1 if you think it was a good article. Thanks!