简体   繁体   English

从 javascript 运行 php 脚本

[英]running a php script from javascript

I am trying to add markers to a google map with coordinates being queried from a mysql database.我正在尝试将标记添加到谷歌地图,并从 mysql 数据库中查询坐标。 the script is shown below, within a file named connection.php.该脚本如下所示,位于名为 connection.php 的文件中。

$query = "SELECT site, latitude, longitude, pollution FROM pollution_monitoring.locations";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
{
    $site      = $row['site'];
    $latitude  = $row['latitude'];
    $longitude = $row['longitude'];
    $pollution = $row['pollution'];

    echo ("addMarker($latitude,$longitude,'<b>$site</b><br/>$pollution');\n");     
}

addMarker() is a JavaScript function that has been defined in the below file addMarker() 是在以下文件中定义的 JavaScript 函数

var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/red.png",
    new google.maps.Size(32, 32), new google.maps.Point(0, 0),
    new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
google.maps.event.addDomListener(window, 'load', initialize);

function addMarker(lat, lng, info) {
    var pt = new google.maps.LatLng(lat, lng);
    bounds.extend(pt);
    var marker = new google.maps.Marker({
        position: pt,
        icon: icon,
        map: map
    });
    var popup = new google.maps.InfoWindow({
        content: info,
        maxWidth: 500
    });
    google.maps.event.addListener(marker, "click", function() {
        if (currentPopup != null) {
            currentPopup.close();
            currentPopup = null;
        }
        popup.open(map, marker);
        currentPopup = popup;
    });
    google.maps.event.addListener(popup, "closeclick", function() {
        map.panTo(center);
        currentPopup = null;
    });
}

function initialize() {
    var mapProp = {
        center: new google.maps.LatLng(-29.335989, 27.483622999999966),
        zoom: 17,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        mapTypeControl: true,
        zoomControl: true,
        scaleControl: true,
        streetViewControl: true,
        overviewMapControl: true,
        rotateControl: true,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
        },
        navigationControl: true,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        }
    };
    $.get('connection.php');

    var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
    google.maps.event.addDomListener(window, 'load', initialize);

the above script is to be used to load a google map and have to markers added to the map.上面的脚本用于加载谷歌地图,并且必须将标记添加到地图中。 I had thought the result of the php script (the addMarker function) would be ran with and have the markers added to the map, but that does not happen.我原以为 php 脚本(addMarker 函数)的结果会运行并将标记添加到地图中,但这并没有发生。 the JavaScript is then included in a jsp file.然后将 JavaScript 包含在一个 jsp 文件中。 The map loads, but no markers are added.地图加载,但没有添加标记。

what am i doing wrong?我究竟做错了什么?

You should rewrite your php script to generate JSON data:您应该重写您的 php 脚本以生成 JSON 数据:

header('Content-Type: application/json');

$query = "SELECT site,latitude,longitude,pollution FROM pollution_monitoring.locations";
$result = mysqli_query($conn,$query);

$i = 0;
echo '[';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
{
    if ($i++ != 0) echo ',';
    echo json_encode($row);    
}
echo ']';

Then add the markers using JavaScript:然后使用 JavaScript 添加标记:

$.getJSON('connection.php', function(data) {
    for (var i = 0; i < data[i]; i++) {
        addMarker(data[i].latitude, data[i].longitude, "<b>" + data[i].site + "</b><br />" + data[i].pollution);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM