Javascript (jQuery) - Hidden Field?

I am writing a web application and had a question regarding the best was to append values to an HTTP Post.

For my get requests, I am using jQuery to append auxiliary data using .load as indicated here

$('#contentPane').load(url, 'subop='+subOp);

As you can see I am sending along extra value’s with my get requests that controller Servlet uses to send back the right data.

In addition to the get requests, I would like to add some extra data to my forms so that I can have Servlet controller perform the appropriate tasks.

Is the best way to do this by adding a hidden field to HTML?

<input type="hidden" name="Language" value="English">

If it helps, here is what I am doing with my jQuery submits …


$('form').live('submit', function(e) {
		var formData = $('form').serialize();
....
....

				$.ajax({
				   type: "POST",
				   url: "/netcomm/controller",
				   //data: "password="+password+"&firstName="+firstName,
				   data: formData,
				   success: function(msg){
					 alert( "Data Saved: " + msg );
				   },
				   error:function (xhr, ajaxOptions, thrownError){
                     alert(xhr.status);
                     alert(thrownError);
                   }  
				 });

Any advice regarding the best way to do this is much appreciated …

Thanks so much!
Aryeh

Put it in array instead of string, like this:


var formData = {'password': password, 'firstName': firstName},
 form = $('form').get(0);
for(var i=0; i<form.elements.length; i++)
{
 formData[form.elements[i].name] = form.elements[i].value;
}

Thanks for the input!

Would it be terrible to just append extra value to the serialized string?


$('form').live('submit', function(e) {
	var formData = $('form').serialize();
        formData = formData + "&option=user";
        ...
        ...

Is this a no-no in terms of best practice?

Thank you,
Aryeh

You can, but I’m not sure what would happen if you’ll append a variable that already exists in that string.