-1

I am trying to pass arrays from PHP into javascript by using json_encode

but when i alert the values i just see "Object object etc"

when i var_dump it i see the actual arrays but its not showing them in the alert

Any help would be appreciated

Regards

this is the var_dump

array(1) {
  [0]=>
  array(2) {
    ["id"]=>
    string(19) "3.0268"
    ["postcode"]=>
    string(137) "hello"
  }
}

array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    string(19) "3.0268070455319E+17"
    ["postcode"]=>
    string(137) "ECMWF continues its flip-flopping, still a temp drop next week & #snow risk but then no rise, http://t.co/tBlg9Ihs #ukweather #uksnow"

} Code

<?php

 $con =  mysql_connect('localhost', 'root', '');
    mysql_select_db('test');

   $result = mysql_query("SELECT * FROM address");

$arr = array();
while($row = mysql_fetch_assoc($result)) {
    $arr[] = $row; 

}

?>

<script>

var test = <?php echo json_encode($arr); ?>;
alert(test);

</script>
Hashey100
  • 994
  • 5
  • 22
  • 47

1 Answers1

5

alert will call toString() on what is passed to it. You might want console.log. test is an object and that is what objects print in alert by default.

Example:

alert({a:1,b:2}) // => [object Object]
({a:1,b:2}).toString() // => "[object Object]"
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445