B4J Question Best way to Handle Posting multiple checkbox items in a web app

miker2069

Active Member
Licensed User
Longtime User
I have a B4J web server app that will receive post data from a web page. The post data includes several checkboxes in which there may be multiple checkboxes checked. I want to submit all the selected checkboxes to my B4J handler class. Note I am submitting the form data using Jquery (and via AJAX as well).

So my HTML checkbox snippet looks like this:

<select id="opt" name="opt">
<option value="foo">Foo</option>
<option value="bar">Bar</option>
<option value="quux">Quux</option>
</select>


I submit post my form via AJAX using the following:

$.ajax({
type: "POST",
url: "/RegisterUser",
data: $("#form1").serialize(),
success: function(result)
{
alert(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});



In my handler that handles RegisterUser, I can get all the form data without a problem using:

dim email as string
email = req.GetParameter("checkemail")

That above obviously works great for regular input boxes.

I tried doing something like the following to get the checked items from the form:

Dim ck1() As String

ck1 = req.GetParameterValues("chk1")
Log("len:" & ck1.Length)


I used GetParameterValues since it seemed to be the best option to get an array of strings. That didn't work.

Something like:

dim chk1 as string

chk1 = req.GetParameter("ck1")

Only (and obviously) gets the first checked item and none of the others. Looking into this, it seems like this is a common issue with checkboxes. The Jquery form serialization doesn't really bundle up checkbox selections for you (which I would've thought it would). Seems like it's up to you (the dev) to serialize on your own and stick it in another hidden field or something to be submitted. Then I suppose I could explode it out in B4J as an array of strings.

I found this post which seems to show how to serialize the checkbox selections:

var array_values = [];
$('input[type=checkbox]').each(function() {
if ($(this).is(':checked')) {
array_values.push($(this).val());
}
});
// Now you can store your values in a comma separated list
var arrayValues = array_values.join(',');


My question is, is this basically the best way? I'm rather new to jquery (and finding it awesome) so I don't know all the ins and outs of it yet. Any feedback would be appreciated!
 

sorex

Expert
Licensed User
Longtime User
there is no need for that loop to check the enabled checkboxes.

when you use an oldskool form submit or jquery serialize it will create a checkbox array that only contains the selected values.

not sure how a B4J app will deal with it but it works fine with PHP.
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
Well I am using a button, and processing the click event to do my jquery validation and submit. From my testing it only sends the first checked value despite having multiple items checked.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
hard to tell without seeing the correct source.

I only see a select in post 1 which is a dropdown menu with only 1 selection possible unless you use multiple in the definition.

for you checkboxes you need to work with array definition like

B4X:
<input type=checkbox name=options[] value=foo>Foo<br>
<input type=checkbox name=options[] value=bar>Bar<br>

then you can iterate through the items. (values should be the ID of the option instead of text in optimal situations)

but as I wrote this is how it works in PHP, I don't know how B4J threats this.
 
Last edited:
Upvote 0
Top