Skip to content Skip to sidebar Skip to footer

How To Send Javascript Array To Servlet Inside Form Tag?

I have made checkbox code and on checkbox click I am calling function which will add passed argument to JavaScript array. I have taken form tag inside body and inside form tag ther

Solution 1:

Here is sample code as Santosh explained,

In your script (Edited),

var acdata = [];
functionaddcategory(acid)
{
    acdata.push(acid);
    $("#hidden_array").val(acdata);
}

In your html,

<input type="checkbox" id="demo_box_2<%= aname %><%= aval %><%=k %>"class="css-checkbox" 
name="configcheckbox" value="<%= aval %>" onchange="addcategory('<%= acid %>')">
<labelfor="demo_box_2<%= aname %><%= aval %><%=k %>"name="demo_lbl_2<%=k %>"class="css-label">&nbsp;<%= aval %></label>

With this add one more hidden field inside your form like this,

<inputtype="hidden"id="hidden_array" name="hiddenArray" >

Hence you can get the array values in your jsp/servlet like this,

request.getParameterValues("hiddenArray");

PS: You can use string arrayString[] to process your data.

Solution 2:

Here is one way to achieve this,

  1. Create a hidden field inside the form,
  2. Convert Javascript array to a comma/pipe separated string, (example 1, 2)
  3. Set the value of the hidden field to this string, (example here)
  4. This way, when you submit the form, the hidden field data is also sent to server,
  5. On the server side, split the string to get the data back as array of Java strings.

Post a Comment for "How To Send Javascript Array To Servlet Inside Form Tag?"