-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
  • 984
  • 5
  • 22
  • 45

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]"
Juan Mendes
  • 85,853
  • 29
  • 146
  • 204
Daniel A. White
  • 181,601
  • 45
  • 354
  • 430