0

Please can someone tell me where I'm going wrong js file:

//Checkboxid is an array which is populated using .push();
$.ajax({
    type: "POST",
    url: "test.php",
    dataType: 'html',
    data: { data: Checkboxid },
    success: function (data) {
        console.log(data);

    }
});

test.php:

<?php
 $test = $_POST['data'];
 for ($i = 0; $i < count($test); ++$i) {
        echo $test[$i];
    }

    foreach($test as $val) {
    echo $val;
}
?>

console.log displays:

ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

4
  • what is result of var_dump($test) Commented Jun 25, 2014 at 10:41
  • array(26) { [0]=> array(1) { [0]=> string(28) "Adverse_Weather_Conditionsv1" } [1]=> array(1) { [0]=> string(21) "Bank_Holiday_Policyv1" } Commented Jun 25, 2014 at 10:42
  • If I do for (var i = 0; i < Checkboxid.length ; i++) { console.log(Checkboxid[i]); } just before the ajax it displays fine. Commented Jun 25, 2014 at 10:45
  • what is the result of console.log(checkboxid) in javascript? Commented Jun 25, 2014 at 10:46

1 Answer 1

1

Your $test variable is a two-dimensional array. Try this

<?php
   $test = $_POST['data'];

  foreach($test as $arr) {
     foreach($arr as $val){
          echo $val;
     }
  }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Haha Thank you so much I started coding this last night came back to it this morning I completely forgot it was 2D. I thought I was going insane.
You should always think about dimensional arrays when your print tests are "Array" :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.