Skip to content
Advertisement

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">
    <thead>
      <tr>
        <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>
      </tr>
    </thead>
    <tbody>
      <?php if ($categories) { ?>
      <?php foreach ($categories as $category) { ?>
      <tr>
        <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>
      </tr>
      <?php } ?>
      <?php } else { ?>
      <tr>
        <td class="center" colspan="4"><?php echo $text_no_results; ?></td>
      </tr>
      <?php } ?>
    </tbody>
  </table>
</form>

<br />
<div align="right">
  <select name="action_select">
    <option value="enable">Enable Selected</option>
    <option value="disable">Disable Selected</option>
  </select>
  <input type="button" class="button" id="update_status" value="Update Status" />
</div>
<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?

    });

</script>

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

Advertisement

Answer

Otherwise you can use the below example code

<script>
    $(document).ready(function()
    {
            $("input[type=checkbox]").click(function()
            {
                    var categoryVals = [];
                    categoryVals.push('');
                    $('#Category_category :checked').each(function() {
                  categoryVals.push($(this).val());
                });
                $.ajax({
                    type:"POST",
                    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
                    } 
                    });
            }
    }
    </script>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement