0

I'm trying to pass a value via PHP to a Javascript function but it isn't working, the value doesn't appear to be going through. When I replace the variable with static text, it works fine.

I'm calling the function via a PHP file like so..

<?php
if (!empty($data['geocode'])) {
    echo '<body onload="initializeGM(\'' . $data['geocode'] . '\');">';
} else {
    echo '<body>';
}
?>

$data['geocode'] contains both the lat and lng as a string.

Here is the Javascript code..

function initializeGM(geocode) {
  var myOptions = {
    zoom: 8,
    center: new google.maps.LatLng(geocode),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

I have added an alert(geocode) into the JS function to test and it works fine.

How can I make it so I can pass the geocode variable in and it works?

2
  • How are you passing the PHP variable to your function? Commented Jan 24, 2012 at 0:23
  • What does $data['geocode'] typically look like? Commented Jan 24, 2012 at 0:35

2 Answers 2

3

Assuming I've understood your question correctly, geocode is a PHP variable and you want to pass that into a JavaScript function call. If that's the case, you need to echo out the value of the PHP variable:

new google.maps.LatLng(<?php echo($geocode); ?>);

However, note that the LatLng constructor expects 2 arguments of type Number (and an optional boolean argument), and you are only trying to pass in one argument. Although I may have completely misunderstood your intentions.

Edit (see comments)

I didn't notice that you were passing geocode as an argument to the initializeGM function. If you were actually trying to pass your PHP variable into that function you need to modify the call to the JavaScript function to use the PHP variable:

initializeGM(<?php echo($geocode); ?>);

The above "however" paragraph still applies though. Your code is likely to throw an error as lng will be undefined in the LatLng constructor function.

Edit again (see edited question)

What you have appears at first glance that it should result in something along these lines (as you've stated that geocode contains a string):

<body onload="initializeGM('somelat,somelng');">

There is nothing wrong with that in theory (a string will be passed into initializeGM) but the problem is that the function expects two arguments of type Number, not one argument of type String:

<body onload="initializeGM(100, 200);"> <!--Use real lat-lng values here!-->

That would require a change to the JavaScript function:

function initializeGM(lat, lng) { //2 arguments
    var myOptions = {
        zoom: 8,
        center: new google.maps.LatLng(lat, lng), //Pass 2 arguments to constructor
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think he's passing the PHP variable in the call to initializeGM(). Could be wrong though
Oh yeah, you may be right. Although to pass in a PHP variable to initializeGM he would need to modify the call to initializeGM, which has not been shown in the question.
@Brett - At first glance your code appears fine, but what you are doing seems to be the wrong approach. See my edit. If I'm wrong, post the generated HTML (view source in the browser and see what's actually been sent to the browser, rather than posting the PHP that generates the HTML).
Yeah... I realized not long after my last edit that I was posting the two arguments as one string. Not sure why I was doing that haha.... mustn't of been concentrating ;) Thanks!! :)
1

The google.maps.LatLng constructor takes a minimum of two arguments, lat:number and lng:number. You are only passing one.

Assuming $data['geocode'] looks something like 1.234,5.678, try this...

  1. Change your JS function to

    function initializeGM(lat, lng) {
      var myOptions = {
        zoom: 8,
        center: new google.maps.LatLng(lat, lng),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }
    
  2. Change the call to

    <?php if (!empty($data['geocode'])) :
    $coords = explode(',', $data['geocode']);
    ?>
    <body onload="initializeGM(<?php printf('%.15f, %.15f', $coords[0], $coords[1]) ?>)">
    <?php else : ?>
    <body>
    <?php endif ?>
    

Comments

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.