Skip to content

How to get the value of selected checkbox in jquery and assign it to a json array?

I have a problem, I have little understanding about jquery that’s why I am having a hard time with my code. I have a pagination of products. And there are checkboxes on it. And my simple goal is to disable the products if the checkboxes are selected. But I can’t do it in one form because I have already another process in my form which is delete products if selected. That’s why I am planning to create a form action in my jquery and pass all the product id from my checkbox to a json array. But how can I do that?

Here’s a bit of my code

<form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
  <table class="list">
        <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*='selected']').attr('checked', this.checked);" /></td>
        <td class="left"><?php echo $column_name; ?></td>
        <td class="right"><?php echo $column_sort_order; ?></td>
        <td class="left"><?php echo $column_status; ?></td>
        <td class="right"><?php echo $column_action; ?></td>
      <?php if ($categories) { ?>
      <?php foreach ($categories as $category) { ?>
        <td style="text-align: center;"><?php if ($category['selected']) { ?>
          <input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" checked="checked" />
          <?php } else { ?>
          <input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" />
          <?php } ?></td>
        <td class="left"><?php echo $category['name']; ?></td>
        <td class="right"><?php echo $category['sort_order']; ?></td>
        <td class="right"><?php echo ($category['status'] == 1 ? 'Enable' : 'Disable'); ?></td>
        <td class="right"><?php foreach ($category['action'] as $action) { ?>
          [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
          <?php } ?></td>
      <?php } ?>
      <?php } else { ?>
        <td class="center" colspan="4"><?php echo $text_no_results; ?></td>
      <?php } ?>

<br />
<div align="right">
  <select name="action_select">
    <option value="enable">Enable Selected</option>
    <option value="disable">Disable Selected</option>
  <input type="button" class="button" id="update_status" value="Update Status" />
<br />

Here’s my jquery sample

<script type="text/javascript">

    $("#update_status").on('click', function(){

        //how can I get the id of my checkboxes and assign it to a json array
        //How can I create a form inside it?



That’s all guys I hope you can understand my question. Thanks.


Otherwise you can use the below example code

                    var categoryVals = [];
                    $('#Category_category :checked').each(function() {
                    url:"<?php echo $this->createUrl('ads/searchresult'); ?>", //url of the action page
                    data:{'category': categoryVals},
                    success : function(response){
                       //code to do somethng if its success