doukun0888 2014-04-12 23:48
浏览 8
已采纳

<?php上出现意外的令牌错误

I'm building a webpage that retrieves a series of latitudes and longitudes from a database and then displays these as markers on a google map. It's all working fine, apart from I receive an Uncaught SyntaxError: Unexpected token < on the opening php tag inside the javascript. Can anyone see why I'm getting this error and offer a way to get past it?

Here's the code in question:

    <script type="text/javascript">
//<![CDATA[
var map;
var bounds = new google.maps.LatLngBounds ();
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8
    };
    map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);
  }

//HERE'S WHERE THE ERROR OCCURS  
<?php
  while ($row = $results->fetch_array())
    echo "addMarker(".$row['latitude'].", ".$row['longitude'].", ".$row['time'].", map);";
?>
    function addMarker(lat, lng, time, map) {
        var latLng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            draggable: false,
        });

        bounds.extend(latLng);

        var contentString = 'Time: ' + time + '
' + 'Latitude: ' + lat + '
' + 'Longitude: ' + lng;

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
        });

        return marker;
    }

  map.fitBounds(bounds);
  google.maps.event.addDomListener(window, 'load', initialize);
  //]]>
</script>
  • 写回答

1条回答 默认 最新

  • duancheng6221 2014-04-12 23:50
    关注

    That's because of CDATA It takes every character literally.

    <?php will generate an error because the parser interprets < as the start of a new XML element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法