1

I have an input type=checkbox that contains id from 1-10. I need to select some check boxes and when I click on a particular click event I need to store all checkboxes values (1-0/ checked-not checked) and ids in 2d array and send to php page using ajax.

<input type="checkbox" id='1' name="ppackages" class="success">
var popularpackages = [];

$("input:checkbox").each(function(){
    var $this = $(this);
      popularpackages.push($this.attr("id"));
});

alert(popularpackages);

The output is 1,2,3,4,5,6,7,8,9,10 But I need as (id,checked/not checked) (1,1),(2,0),.....(10,0)

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Nandani
  • 21
  • 3
  • Do you mean you need `[{"id1":"1"},{"id2":"0"},{"id3":"0"},...]` format or do you need a flat object like `{1:0,2:1,...}`? What exactly is the problem with @RoryMcCrossan suggestion? – Fr0zenFyr Jan 04 '19 at 11:38

1 Answers1

0

To achieve this you can use map() to build the array, then return the id and checked properties as a sub array, like this:

var popularpackages = $("input:checkbox").map(function() {
  return [[parseInt(this.id, 10), this.checked ? 1 : 0]];
}).get();

console.log(popularpackages);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="1" name="ppackages" class="success" checked="checked" />
<input type="checkbox" id="2" name="ppackages" class="success" />
<input type="checkbox" id="3" name="ppackages" class="success" />
<input type="checkbox" id="4" name="ppackages" class="success" />
<input type="checkbox" id="5" name="ppackages" class="success" checked="checked" />
<input type="checkbox" id="6" name="ppackages" class="success" />
<input type="checkbox" id="7" name="ppackages" class="success" />
<input type="checkbox" id="8" name="ppackages" class="success" />
<input type="checkbox" id="9" name="ppackages" class="success" checked="checked" />
<input type="checkbox" id="10" name="ppackages" class="success" />

You can then send the popularpackages array to your PHP logic using $.ajax, or any of jQuery's other AJAX methods, as you normally would.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • What is 10 in this.id, 10 ? – Nandani Jan 04 '19 at 11:25
  • That's the radix: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Syntax – Rory McCrossan Jan 04 '19 at 11:26
  • Okay, thats fine. the output is correct. I can create the object of popularpackages and send the same object to php page using ajax. as var objdata = new FormData(); objdata.append("popularpackages ",popularpackages ); – Nandani Jan 04 '19 at 11:31
  • I meant to say I also need to use multiple update query in php to update the id with values – Nandani Jan 04 '19 at 11:32
  • I'd ask a separate question about that, as it concerns handling the request in PHP. This question is about creating the array to be sent through JS. – Rory McCrossan Jan 04 '19 at 11:39
  • In short, you can simply do a `json_decode($_POST['popularpackages], true)` and then work with the resulting `php array` – Fr0zenFyr Jan 04 '19 at 11:42
  • Yes. It builds a 2D array, as your question specfies – Rory McCrossan Jan 04 '19 at 16:41
  • OKay, sorry... Please have a look on this too https://stackoverflow.com/questions/54038849/how-to-use-multiple-update-query-using-prepare-statements-in-php-for-storing-the/54039026?noredirect=1#comment94913385_54039026 – Nandani Jan 04 '19 at 17:12
  • I'd love to help with that, but I'm not a PHP dev – Rory McCrossan Jan 04 '19 at 17:14