0

I try pass checkbox array to ajax. My html code:

   {foreach from =$allTaskTypes key=typeId item=type}
                  <div class="mainDivTaskType">
                      <input class="task_types" value="{$typeId}" name="task_types_category[{$typeId}][]" type="checkbox">{$type.type_name}<br>
                      {if $type.subtags neq ''}
                          <div class="taskTypesLeadNoteTagDiv">
                              {foreach from =$type.subtags key=tagId item=tag}
                                  <input  value="{$tag.id}" class="task_types" type="checkbox" name="task_types_category[{$typeId}][task_types_tags][]">{$tag.name}<br>
                              {/foreach}
                          </div>
                      {/if}
                  </div>
              {/foreach}

So, example of html is:

<div class="mainDivTaskType">
    <input class="task_types" value="42" name="task_types_note_category[42][]" type="checkbox">
u7utu
<br>
</div>
<div class="mainDivTaskType">
<input class="task_types" value="43" name="task_types_note_category[43][]" type="checkbox">
New1
<br>
<div class="taskTypesLeadNoteTagDiv">
<input class="task_types" value="28" name="task_types_note_category[43][task_types_tags][]" type="checkbox">
tag1
<br>
<input class="task_types" value="29" name="task_types_note_category[43][task_types_tags][]" type="checkbox">
tag2
<br>
</div>
</div>

I need to pass array from js to php. If I use submit, I get array such as:

    Array
(
    [42] => Array
        (
            [0] => 42
        )

    [43] => Array
        (
            [task_types_tags] => Array
                (
                    [0] => 28
                    [1] => 29
                )

        )

)

How can I generate such array using ajax?

I tried it:

   var array = [];
      $('.task_types:checked').each(function() {
          array.push($(this).val());
      });

But this is wrong array format, like [42,28,29]

How can I solve this problem?

1 Answer 1

1

Try this:

$(".task_types:checked").serialize();
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.