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!
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!