SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code JavaScript:
    $('#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?

    Code HTML4Strict:
    <input type="hidden" name="Language" value="English">

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

    Code JavaScript:
    $('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

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put it in array instead of string, like this:
    Code:
    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;
    }

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input!

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

    Code JavaScript:
    $('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

  4. #4
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can, but I'm not sure what would happen if you'll append a variable that already exists in that string.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •