0

I intend to pass javascript array to the server. this array will basically contain all the option values of a select tag with multiple option.

Client side

<input type="hidden" id="selectedGroupIds" name="selectedGroupIds">
<input type="submit" name="mapSubmit" value="Map Now" onclick="setGroupIds()">

function setGroupIds() {
    var selectedGroupIds = [];
    $('#selectedGroups option').each (function() {
        selectedGroupIds.push($(this).val());
    });
    $('#selectedGroupIds').val(selectedGroupIds);
}

Server side

String[] groupArr = request.getParameterValues("selectedGroupIds");
System.out.println("Length = " + groupArr.length); // Prints 1 even if 2 elements in the array like [1,2]

Update I knw it can be done by getParameter() and split. Just curious to know if can be done without split by using getParameterValues()

2 Answers 2

1
String selectedGroups = request.getParameter("selectedGroupIds");
String[] arr = (selectedGroups!=null)?selectedGroups.split(","):null;
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry forgot to mention this solution is known. I edited my question and updated also. Want to do it without splitting.
0

get parameter values is to be used when you have a single parameter with multiple values. You think you have accomplished this by setting your array as the val() of the input field, however you have actually done an implicit join (or implode) type command on your array into a single string, a comma delimited list, so getparametervalues will always just get one value.

a situation to use it to get multiple values, would be something like

<input type=checkbox name=check1 value='1'>
<input type=checkbox name=check1 value='2'>
<input type=checkbox name=check1 value='3'>

to get all the different values checked for the group of checkboxes called check1

1 Comment

Not at all, I've been there! Hopefully this post helps others who might find themselves in a similar situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.