在javascript中打印PHP值

Is it possible to echo PHP in a javascript? i want to echo google coordinates in a javascript to center the map. The coordinates comming from the database (PhpMyAdmin)

The results from the echo are 50.9272511, 4.425786799999969

Check the following code out:

<script type='text/javascript'>
   function init_map() {
       var myOptions = {
            zoom: 10
            , center: new google.maps.LatLng(<?php echo $box['coordinates'];?>)
            , mapTypeId: google.maps.MapTypeId.ROADMAP
       };
       map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);

       //1
       marker = new google.maps.Marker({
            map: map,
            position: new google.maps.LatLng()
       });
       infowindow = new google.maps.InfoWindow({
            content: '<strong>TEST</strong><br>Den helder, nederland<br>'
            });
       google.maps.event.addListener(marker, 'click', function () {
                infowindow.open(map, marker);
            });
            infowindow.open(map, marker);
     }

     google.maps.event.addDomListener(window, 'load', init_map);
</script>

Try to do like below:

In php : Separate latitude and longitude in different variable

$latLong = '50.9272511,4.425786799999969';
list($lat, $long) = explode(',',$latLong);

In JS : pass latitude and longitude as a separate parameter

<script type='text/javascript'>
    function init_map() {
        var myOptions = {
            zoom: 10
            , center: new google.maps.LatLng('<?php echo $lat;?>','<?php echo $long;?>')
            , mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('gmap_canvas'), myOptions);

        //1
        marker = new google.maps.Marker({
            map: map
            , position: new google.maps.LatLng()
        });
        infowindow = new google.maps.InfoWindow({
            content: '<strong>TEST</strong><br>Den helder, nederland<br>'
        });
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });
        infowindow.open(map, marker);
       google.maps.event.addDomListener(window, 'load', init_map);
</script>

Try this one

new google.maps.LatLng('<?php echo $box["coordinates"];?>');

I guess you can, if answer above doesn't work, put your vaule in var before use it.

var coord = <?= $box["coordinates"] ?>

  1. If you incorporate the JS script in your HTML interpreted file (as you did in your example) than you have to be sure that your code echoes exactly what you should write manually: here, I imagine something like 50.927,4.425
  2. I would recommand to put your JS code in a .js file. However, depending on your HTTP server configuration, I'm not sure that the .js JavaScript file would be interpreted like any other .php file, even if it incorporates PHP commands. Hence, you could generate and use variables stored inside your HTML document like settings, at some point in your code by incorporating something like

    <script type='text/javascript'>
        var long = 50.927;
        var lat = 4.425;
    </script>
    

    and use them in your init_map() function (as parameters, for example).