I have a checkbox with multidimensional array which I was using with php submiting. Now I'm rebuilding my code to use ajax.
<li class="formVariationParent">
<input checked="" name="variationParent[]" autocomplete="off" value="3" type="checkbox">colors
<ul>
<li class="formVariationChild">
<input checked="" name="variationChild[edit_var_val][3][4]" autocomplete="off" value="1" type="checkbox">red</li>
</ul>
<ul>
<li class="formVariationChild">
<input checked="" name="variationChild[edit_var_val][3][5]" autocomplete="off" value="1" type="checkbox">green
</li>
</ul>
<ul>
<li class="formVariationChild">
<input checked="" name="variationChild[edit_var_val][3][6]" autocomplete="off" value="1" type="checkbox">blue
</li>
</ul>
And this is how it's generated:
<fieldset>
<a class="btn btn-new btn-primary" onclick="apfGenerateVariation(<?php echo $current_post ?>,<?php echo $get_admin_blog ?>,data);" style="cursor: pointer">
<b><i class="icofont-star"></i></b>
</a>
<ul class="formVariations" id="formVariationsID"><li>
<?php
$parentArray = array();
foreach ($parentVariations as $variation) {
$parentArray[] = $variation->term_id;
}
foreach($variationCategories as $category) {
//var_dump($category);
//echo ;
// check if this variation is already checked
if (in_array($category->term_id, $parentArray)) {
if ($category->parent == 0) {
echo "</li><li class='formVariationParent'><input type='checkbox' checked name='variationParent[]' autocomplete='off' value='". $category->term_id ."'>" . $category->name . "";
} else {
echo "<ul><li class='formVariationChild'><input type='checkbox' checked name='variationChild[edit_var_val][". $category->parent ."][". $category->term_id ."]' autocomplete='off' value='1'>" . $category->name . "</li></ul>";
}
} else {
if ($category->parent == 0) {
echo "</li><li class='formVariationParent'><input type='checkbox' name='variationParent[]' autocomplete='off' value='". $category->term_id ."'>" . $category->name . "";
} else {
echo "<ul><li class='formVariationChild'><input type='checkbox' name='variationChild[edit_var_val][". $category->parent ."][". $category->term_id ."]' autocomplete='off' value='1'>" . $category->name . "</li></ul>";
}
}
}
?>
</ul>
</fieldset>
Now I want to select this variationChild with jQuery and save in variable data as if it was submited.
In input name second parenteses are parentID and third childID. I have thouse variables in php so I can rebuild this input however I want but in the and checked data should look like this:
array(1) {
["edit_var_val"]=> array(2) {
[3]=> array(3) {
[6]=> string(1) "1"
[5]=> string(1) "1"
[4]=> string(1) "1"
}
[12]=> array(3) {
[13]=> string(1) "1"
[14]=> string(1) "1"
[15]=> string(1) "1"
}
}
}
Here first array is this inputs with parentID 3 and second, the continue of this form, other ParentID.
This is a jQuery I try to use.
var data = { 'edit_var_val[]' : []};
jQuery(".formVariationChild input:checked").each(function() {
data['edit_var_val[]'].push(jQuery(this).attr('name'));
});
I'm using jQuery .map() and .get() functions, but I guess this is different case.
So how to get checked input and save in variable as this array?