0

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?

5
  • It seems odd to have only li in several diffrent ul's. Could you not do like <ul><li>red...</li><li>green...</li></li>blue....</li> ? But besides from that. How does you js-code look like and what are you trying to achieve? Commented Apr 15, 2013 at 21:53
  • I added php how it generated. at the beggining of this code there is anchor tag with onclick function. here I pass data variable, I want this data to container all this checked values in the same order as array in the example. Commented Apr 15, 2013 at 22:01
  • It seems like your building some kind of menu? In your question you atate "Now I'm rebuilding my code to use ajax." What exactly do you mean by that? How does your current jQuery code look like? Commented Apr 15, 2013 at 22:07
  • It's a veeeeery big code, actually it a function that takes data in that order. It's a wordpress, with some shop plugin and I'm building dashboard "add to products" to fronted. It's really hard to understand what I want right? I'm not good at describing things. And I wanted to keep it simple. This checkboax represent childPosts which should be genereted after clicking this first anchor And generate var data; jQuery(".formVariationChild input:checked").each(function() { //here I try to push array to var data }); Commented Apr 15, 2013 at 22:15
  • Ok, look at stackoverflow.com/questions/15745927/… on how to get the values, and then serialize values and send them with AJAX/JSON to php-file. Commented Apr 15, 2013 at 22:20

1 Answer 1

0

If I understand you correctly, you're looking for something like this?

var data = {}; //create an object (instead of an array)
$('.formVariationChild input[type=checkbox]').each(function() {
    data[$(this).attr('value')] = $(this).attr('checked');
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.