-1

How can I pass selected checkbox values to another servlet (not below one) using jQuery post method?

This is my servlet code for generationg check boxes on the html page I am using json

for(int i=0;i<roles.size();i++){
                 JSONObject msg = new JSONObject();
                msg.put("selector", "#roles");
            //<input type=radio name="radio" value="<%=bean.getSno()%>"></td>
    msg.put("msg", 
            "<input type=checkbox id="+'"'+"checkedroles"+'"'+"name="+'"'+"checkedroles"+'"'
            +"value="+roles.get(i)+">"+roles.get(i)+"<br>");
    messages.put(msg);

            }

this is the jquercode

* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
  //global vars
  var UserName = $("#UserName"); //user name field
  var FirstName = $("#FirstName"); //first name
  var LastName=$("#LastName");
  var StreetAdress=$("#StreetAdress");
  var City=$("#City");
  var Province=$("#Province");
  var Organization=$("#Organization");
  var email=$("#email");
  var phone=$("phone");
  var checkedroles=$("#checkedroles");
  var selected = new Array();

    function checkCommentsForm(){
   return true;
  }
  $("input:checkbox[name=checkedroles]:checked").each(function() {
       selected.push($(this).val());
  });


  //When form submitted
  $("#Reg").submit(function(){
    if(checkCommentsForm()){
      $.ajax({
        type: "post",
        url: "loginProcess.jsp",
        data: {user : UserName.val(), fname : FirstName.val(),lname : LastName.val()
    ,stAddress:StreetAdress.val(),city:City.val(),prov:Province.val(),org:Organization.val()
    ,mail:email.val(),ph:phone.val(),'ch[]':selected},
        success: function(data) {
                                    }

});
        }

  });
});

also pls tell me how to retrieve the array in the servlet

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ahsan
  • 11
  • 4

1 Answers1

2

You haven't shown your HTML but from the looks of it, it seems like these values are in a form.

You could then use ('#Reg').serialize() instead. This will Encode a set of form elements as a string for submission. - it will include all successful controls so all checked checkboxes will be included automatically as will all enabled text fields.

$("#Reg").submit(function(){
    if(checkCommentsForm()){
      $.ajax({
            type: "post",
            url: "loginProcess.jsp",
            data: $(this).serialize(),
            success: function(data) {
                    }
        });
    }
});

To retrieve the submitted values in your servlet, use the ServletRequest#getParameter() and ServletRequest#getParameterValues() methods. The former for when you have just one value for a parameter (for example, a test field) and the latter when you can have multiple values with that parameter name, for example, multiple checkboxes with the same name as is the case in your question. So you'd write something like this in your servlet:

String [] checked = request.getParameterValues("checkboxname");
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51