How to add multiple checkboxes

In is quite common to use checkboxes to collect “multiple” values. For example one can ask about favorite cars and use checkboxes with different car brands and the same field name e.g. “cars”. When user submits form value of this field will be a comma separated list of selected cars. When I tried to set the same name on dhtmlx checkbox control second checkbox was automatically renamed to get new unique name and of cause I lost ability to collect multiple values from the field.

Is it any way to achieve that functionality using dhtmlx checkbox or other form controls? I know about multiselect field but it is much less convenient then multiple checkboses.

Best Regards
Greg

Hi

not sure I understood correctly, but:

JS

formData = [ {type: "settings", position: "label-right", labelWidth: 130, inputWidth: "auto"}, {type: "checkbox", name: "brand[audi]", label: "Audi", checked: true}, {type: "checkbox", name: "brand[bwm]", label: "BMW"}, {type: "checkbox", name: "brand[volkswagen]", label: "Volkswagen"} ]; myForm = new dhtmlXForm("myForm", formData); myForm.send("1.php", function(r){ console.log(r.xmlDoc.responseText); });

1.php

<?php print_r($_REQUEST["brand"]); foreach ($_REQUEST["brand"] as $car => $checked) { if ($checked) { // do some } } ?>

response

Array ( [audi] => 1 [bwm] => 0 [volkswagen] => 0 )

Hi Andrei,

I think your example works only half way. It allows to get data in format I would expect in php because it has some special processing rules for names with square brackets. To my knowledge nothing like that works in .NET (or other server site languages) where Request[“brand”] will be empty and one will need to write a special code to say that Request[“brand[bmw]”] is somehow related to Request[“brand[audi]”]. What is more there is no way to treat “brand” checkboxes as standard html checkboxes during loading data where I would use xml string like:

<data> <brand>Audi, BMW</brand> </data>
It looks like some standard html functionality was removed from dhtmlx checkboxes for some reason. It would be acceptable if it was a way to achieve the same thing using some other control (which uses checkboxes) but I can’t see how. I can see that multiselect dhtmlx control http://docs.dhtmlx.com/form__controls_list.html#multiselect works this way (which is also standard html way by the way) so I am puzzled why it is impossible to do in checkbox control.

Hi

example for dot_net:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace dhtmlxForm_post_array.Controllers { public class FormModel { public Dictionary<string, int> brand { get; set; } } public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } public ActionResult Save(FormModel form) { foreach (var brand in form.brand) { if (brand.Value == 1) { // brand.Key => "audi" (or other checked..) } } return new EmptyResult(); } } }

or you can send your custom data to a server:

var data = myForm.getFormData(); // this will give you { brand[audi]: 1, brand[bwm]: 0, brand[volkswagen]: 0 } data = myCustomDataFormatter(data); dhx4.ajax.post("url", data, function(r){ // callback });