-1

I'm trying to pass an array from javascript to PHP. The error message says:

NULL Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/jsontest/json_receive.php on line 10

Anything wrong with my code?

<script>
function sendData(){
    var arr= [{  
        "city" : "Brussels",  
        "age" : 25  
    },
    {  
        "city" : "Antwerp",  
        "age" : 40  
    }]; 

    $.ajax({
       type: "POST",
       url: "json_receive.php",
       datatype: 'JSON',
       data: {arr: JSON.stringify(arr)},
       success: function(data){
            console.log("success:",data);
        },
       failure: function(errMsg) {
            console.error("error:",errMsg);
       }
    });
}

<?php
$data = json_decode($_POST["arr"]);
// will echo the JSON.stringified - string:
echo $_POST["arr"];
// will echo the json_decode'd object
var_dump($data);
//traversing the whole object and accessing properties:

print_r($data);
foreach($data as $cityObject){
    echo "City: " . $cityObject->city . ", Age: " . $cityObject->age . "<br/>";
}
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Qin Lin
  • 13
  • 2
  • And what does all the echoing, dumping and printing show you ? – adeneo Oct 12 '15 at 12:58
  • try `$data = json_decode($_POST["arr"],true);` – sandeepsure Oct 12 '15 at 12:58
  • Have you watched the request / response in the browser's console? – Jay Blanchard Oct 12 '15 at 13:00
  • What if you just change `data: {arr: JSON.stringify(arr)}` into `data: {arr : arr}`. – ThinkTank Oct 12 '15 at 13:01
  • 2
    I've tested your code on my server as it works as expected, no errors. – Pedro Lobito Oct 12 '15 at 13:07
  • @QinLin are you still having problems with your code? – CodeGodie Oct 12 '15 at 13:16
  • what you are getting by `var_dump`?? – Rohit Kumar Oct 12 '15 at 13:25
  • 1
    What re you trying to achieve with this code ? it seems you're passing the same data back and forward. Also, as I said previously, **there's nothing wrong with your code**. – Pedro Lobito Oct 12 '15 at 13:32
  • 1
    I agree with @PedroLobito , your code seems fine. – CodeGodie Oct 12 '15 at 13:33
  • @PedroLobito I am trying to use the code to test how I can pass an array to PHP from javascript. When I run the PHP code. I still got nothing but "NULL Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/jsontest/json_receive.php on line 10". If it works as expected, some data should be printed in the page right? Or is it I am using localhost so I cannot get it correct? Anw thank you guys, I am new to PHP, please bear with me if my questions are too naive. – Qin Lin Oct 12 '15 at 14:51
  • check the console, nothing will be printed because you're just logging to the console. – Pedro Lobito Oct 12 '15 at 14:52

1 Answers1

0

As I said on the comments, You're code looks good and works (I've tested it), the problem is that you're expecting something to be printed on the page but your Ajax success function only uses the console.

   success: function(data){
        console.log("success:",data);
    }
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • The console says "Uncaught ReferenceError: $ is not defined", referring to the line $.Ajax ({ – Qin Lin Oct 12 '15 at 15:08
  • It seems that your jquery isn't included correctly ` ` – Pedro Lobito Oct 12 '15 at 15:10
  • Yeah, the javascript file works as expected. Still got nothing printed out on the php page even if I commented the 'console' statements. Is there a better method for me to print the data in php? – Qin Lin Oct 12 '15 at 15:51
  • You need to get the result from the php and assign it to any element of your page , check a sample of that http://stackoverflow.com/a/18064824/797495 – Pedro Lobito Oct 12 '15 at 15:54
  • I intend to go to the php page with datas after executing the javascript. However, now the data all not there when I go to php page. How can I achieve this? Can you advise? – Qin Lin Oct 13 '15 at 10:24