1

I am trying to pass a php array to a javascript variable as an object to use in google maps on the same page/file. I am not able to send out an alert when testing the array in javascript.

PHP

while( $row = $query->fetch_assoc() ){
$street_address = $row['street_address'];
$zip = $row['zip'];
$state = $row['state'];
$lat = $row['lat'];
$lng = $row['lng'];
$test = $row['sellerDB_test'];

$firstName = $row['first_name'];
$lastName = $row['last_name'];
$email = $row['email'];
$phone = $row['phone'];

/* Each row is added as a new array */
$locations = array( 'streetAddress'=>$street_address, 'state'=>$state, 'zip'=>$zip, 'lat'=>$lat, 'lng'=>$lng, 'test'=>$test, 'first name'=>$firstName, 'last name'=>$lastName, 'email'=>$email, 'phone'=>$phone);

JS

var map;
        var Markers = {};
        var infowindow;
        var locations = '<?php echo json_encode($locations); ?>';
        var location = JSON.parse(loactions);
        alert(locations[0]);

I am getting this error

Uncaught ReferenceError: loactions is not defined at account:299

9
  • JS code is in php file with <script> tag? Commented Dec 8, 2017 at 4:46
  • @HimanshuUpadhyay yes! Commented Dec 8, 2017 at 4:46
  • check your locations, which line is account:299? Commented Dec 8, 2017 at 4:47
  • 1
    are you expecting multiple rows? initialize $locations outside the loop, the continually push inside while, $locations[] = array(... blah blah data), then outside use the echo. inside js, no need for quotations, just outright echo it: var locations = <?php echo json_encode($locations); ?>;, so that you don't need JSON.parse Commented Dec 8, 2017 at 4:48
  • @user3780248 are you following the answers? Commented Dec 8, 2017 at 4:58

1 Answer 1

2

@Ghost is right. I did not notice that the $locations is inside while loop. So You should define $locations = []; before while loop. And then keep adding multiple records from while loop. So the updated code should be like:

$locations = [];
while( $row = $query->fetch_assoc() ){
    $street_address = $row['street_address'];
    $zip = $row['zip'];
    $state = $row['state'];
    $lat = $row['lat'];
    $lng = $row['lng'];
    $test = $row['sellerDB_test'];

    $firstName = $row['first_name'];
    $lastName = $row['last_name'];
    $email = $row['email'];
    $phone = $row['phone'];

    /* Each row is added as a new array [] */ 
    $locations[] = array( 'streetAddress'=>$street_address, 'state'=>$state, 'zip'=>$zip, 'lat'=>$lat, 'lng'=>$lng, 'test'=>$test, 'first name'=>$firstName, 'last name'=>$lastName, 'email'=>$email, 'phone'=>$phone);
}

And after this, you should put the JS code snippet.

And use it like this:

JS cpde:

var map;
var Markers = {};
var infowindow;
var locations = <?php echo json_encode($locations); ?>;
var location = JSON.parse(loactions);
alert(location.streetAddress);
3
  • Thanks! That helped a lot but now when I try to send out an alert I get a [Object Object]. I tried alert(locations.streetAddress) and alert(locations[0]) Commented Dec 8, 2017 at 5:02
  • @user3780248, you are storing parsed json data to location not locations so access the data like location.streetAddress and you will get the values. Commented Dec 8, 2017 at 5:04
  • Your welcome @user3780248. Happy to help. Updating my answer so it will be helpful to others. Commented Dec 8, 2017 at 5:10

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.