I have to handle a form generated by a third part. This form provides some checkbox "multi-select" attributes. But input name's are identical, and PHP $_POST can't handle identical keys (only the last data is handled).
Here is an example of the form :
<form>
<input type="checkbox" name="attr1" value="1" />
<input type="checkbox" name="attr1" value="2" />
<input type="checkbox" name="attr1" value="3" />
<input type="checkbox" name="attr1" value="4" />
<input type="checkbox" name="attr2" value="xx" />
<input type="checkbox" name="attr3" value="a" />
<input type="checkbox" name="attr3" value="b" />
<input type="checkbox" name="attr3" value="c" />
<input type="checkbox" name="attr3" value="d" />
<input type="text" name="attr4" value="" />
</form>
<form>
<input type="checkbox" name="attr1[]" value="1" />
<input type="checkbox" name="attr1[]" value="2" />
<input type="checkbox" name="attr1[]" value="3" />
<input type="checkbox" name="attr1[]" value="4" />
<input type="checkbox" name="attr2" value="xx" />
<input type="checkbox" name="attr3[]" value="a" />
<input type="checkbox" name="attr3[]" value="b" />
<input type="checkbox" name="attr3[]" value="c" />
<input type="checkbox" name="attr3[]" value="d" />
<input type="text" name="attr4" value="" />
</form>
This will append []
to the name of each checkbox item if there exists another item with the same name.
var name_map = {};
$("input[type=checkbox]") // for all checkboxes
.each(function() { // first pass, create name mapping
var name = this.name;
name_map[name] = (name_map[name]) ? name + "[]" : name;
})
.each(function() { // replace name based on mapping
this.name = name_map[this.name];
});