意外的令牌< PHP中的Javascript代码

时间:2016-09-01 10:17:06

标签: javascript php

您好我有一个包含下一个代码的文件,一个名为google-maps.php的文件

<script>
    var customIcons = {
        restaurant: {
            icon: 'img/homepage/pin.png'
        },
        cafenele: {
            icon: 'img/homepage/pni2.png'
        }
    };


    var markerGroups = {
        "restaurant": [],
        "cafenele": []
    };



    function load() {
        var map = new google.maps.Map(document.getElementById("map"), {
            center: new google.maps.LatLng(47.059516, 21.947613),
            zoom: 13,
            scrollwheel: false,
            mapTypeId: 'roadmap'
        });
        var infoWindow = new google.maps.InfoWindow();
        // Change this depending on the name of your PHP file

        var bounds = new google.maps.LatLngBounds();
        downloadUrl("googlemaps/phpsqlajax_genxml2.php", function (data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                var id_marker = markers[i].getAttribute("id_marker");
                var name = markers[i].getAttribute("name");
                var category = markers[i].getAttribute("category");
                var address = markers[i].getAttribute("address");
                var img = markers[i].getAttribute("img");
                var phone = markers[i].getAttribute("phone");
                var schedule = markers[i].getAttribute("schedule");
                var link = markers[i].getAttribute("link");
                document.getElementById('categorii').src = img;
                var type = markers[i].getAttribute("type");

                var point = new google.maps.LatLng(
                        parseFloat(markers[i].getAttribute("lat")),
                        parseFloat(markers[i].getAttribute("lng")));
                bounds.extend(point);
                var html = "<u class='title-google-maps'>" + name + "</u> <p class='google-maps'>" + category + "</p>" + address + "<p class='google-maps'>" + phone + "</p>" + "<p class='google-maps-2'>" + schedule + "</p>" + link;
                var icon = customIcons[type] || {};
                var marker = new google.maps.Marker({
                    map: map,
                    position: point,
                    icon: icon.icon,
                    id_marker: id_marker,
                    image: img,
                    type: type

                });

                if (!markerGroups[type])
                    markerGroups[type] = [];
                markerGroups[type].push(marker);
                marker.setVisible(false);
                bindInfoWindow(marker, map, infoWindow, html);

            }
            map.fitBounds(bounds);
            toggleGroup("spa");

        });

    }


    function toggleGroup(type)
    {

        for (var key in markerGroups)
        {
            for (var i = 0; i < markerGroups[key].length; i++)
            {
                var marker = markerGroups[key][i];

                if (type == key) {

                    marker.setVisible(true);

                } else
                {
                    marker.setVisible(false);
                }
            }

        }
    }





    function bindInfoWindow(marker, map, infoWindow, html) {
        google.maps.event.addListener(marker, 'click', function () {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);


            document.getElementById('categorii').src = marker.image;

        });
    }

    function downloadUrl(url, callback) {
        var request = window.ActiveXObject ?
                new ActiveXObject('Microsoft.XMLHTTP') :
                new XMLHttpRequest;

        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                request.onreadystatechange = doNothing;
                callback(request, request.status);
            }
        };

        request.open('GET', url, true);
        request.send(null);
    }

    function doNothing() {
    }
</script>

如果我把我的代码放在JS中,代码可以运行,但我将来需要放入PHP文件,我希望从数据库中回显一些数据。我在控制台中遇到的错误是

Uncaught SyntaxError: Unexpected token< 
。如果文件类型是JS,为什么不工作PHP文件类型,为什么工作?我做了什么?

1 个答案:

答案 0 :(得分:2)

您需要在脚本之前关闭php标记,然后在此脚本之后打开

<?php

  //some php code
?>
 <script>
  ...
  ..
</script>

<?php
 //php code
?>