0

I can't seem to get the PHP to echo out within a Javascript tag:

places.push(new google.maps.LatLng("<?php echo the_field('lat', 1878); ?>"));

What am I doing wrong?

8
  • Does the function (the_field('lat', 1878)) work correctly? Commented Sep 21, 2011 at 14:13
  • is the file extension of your page php? Commented Sep 21, 2011 at 14:13
  • Yep, it's running through wordpress, so definitely. Commented Sep 21, 2011 at 14:15
  • Yes, when placed within normal html it echo's out fine. Commented Sep 21, 2011 at 14:15
  • Post a more complete example, or try to reproduce it on a smaller scale and post that. Commented Sep 21, 2011 at 14:16

5 Answers 5

2

Have you tried it without the quotes ?

places.push(new google.maps.LatLng(<?php echo the_field('lat', 1878); ?>));
Sign up to request clarification or add additional context in comments.

Comments

2

PHP works when you execute the page, and it should work. Please note that php does not execute when you run a JS function. Please also make sure that you really have that the_field function.

3 Comments

the_field exists and echo out when just within html. I just want to echo the field within the javascript, surely that's possible right?
view source in the browser to see what it output
+1 for "php does not execute when you run a JS function". I could see a new developer easily making this mistake.
1

I suspect you have your " quotes in the LatLng method arguments when there shouldn't be any.

Your php should output a string such as '50.123123123, 12.123144' (without the ' quotes). The LatLng method expects 2 values.

places.push(new google.maps.LatLng(<?php echo the_field('lat', 1878); ?>));

Try that.

Comments

0

If you're looking to populate a Google Map with markers as the result of a database query, you probably want to wrap it in a JSON web service. Something as simple as:

<?php
// file to query database and grab palces
// do database connection
$places = array();
$sql = "SELECT * FROM places";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
    $places[] = $row;
}

header('Content-Type: application/json');
echo json_encode($places);
exit;

And then in your JavaScript file:

// get places
$.getJSON('getplaces.php', function(response) {
    for (var i = 0; i < response.length; i++) {
        place[] = response[i];
        places.push(new google.maps.LatLng(place.lat, place.lng));
    }
});

Comments

-1

If i understand what you are trying to do (maybe some more explanation of your problem is required for this), then this might be your answer:

places.push(new google.maps.LatLng(<?php echo '"' . the_field('lat', 1878) . '"' ?>));

EDIT: removed the ;

2 Comments

That just gives me the error: Parse error: syntax error, unexpected '.' in /home/medicom/public_html/mapping/wp-content/themes/default/page.php on line 43
@Rob: this was the typo by the answerer. Try removing the unnecessary ;.

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.