0

I have a function which gets the value of each checked checkbox i was able to get the value successfully by using an alert example it results to 1,2,3 which is correct but when i get it from php the array size is always 1.

HTML CODE:

function doit() {
    var p = [];
    $('input.cb').each(function () {
        if ($(this).is(':checked')) {
            p.push($(this).attr('rel'));
        }
    });
    $.ajax( {
          url:'page.php',
          type:'POST',
          data: {list:p},
          success: function(res) {
               alert(res);
          }
    });
    alert(p)
} 

PHP CODE:

<?php
  $list = $_POST['list'];
  echo count($list);
?>
Farnabaz
  • 4,030
  • 1
  • 22
  • 42
zxc
  • 1,504
  • 3
  • 13
  • 32

1 Answers1

1

Use this code :

var jsonData = JSON.stringify(p);

$.ajax( {
      url:'page.php',
      type:'POST',
      data: {list:jsonData},
      success: function(res) {
             alert(res);
      }
});

And in PHP :

$list = json_decode($_POST['list']);
Jérôme
  • 292
  • 1
  • 6