始终清空$ _POST

时间:2014-04-05 09:48:16

标签: php jquery google-maps

用户放置标记后,我正在尝试将Google Maps Marker的坐标保存到我的数据库中。当我将坐标发送到服务器时,我总是收到一个空的$ _POST数组。 我哪里出错了?

这是我的index.php:

<!DOCTYPE html>
<html>
    <head>
        <title>MyMap</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">

        <link rel="stylesheet" href="css/main.css">

        <style>
            html, body, #map-canvas {
                height: 100%;
                margin: 0px;
                padding: 0px
            }
        </style>

        <script src="http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key=AIzaSyCZseZXpWTYc2Z2aI4mJEtoLz8WUYQCITM"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

        <script>
            var loc;
            var map;
            function initialize() {
                var mapOptions = {
                    zoom: 11,
                    center: new google.maps.LatLng(46.2428219,20.14682)
                };

                map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

                google.maps.event.addListener(map, 'click', function(event) {
                    placeMarker(event.latLng);
                    var lat = event.latLng.lat();
                    var lng = event.latLng.lng();

                    $.ajax({
                        type: "POST",
                        url: "index.php",
                        data: lat,
                        success: function (data) {
                                console.log(data);
                        }
                    });
                });

                function placeMarker(location) {
                    var marker = new google.maps.Marker({
                        position: location,
                        map: map
                    });
                    map.setCenter(location);

                }
            }

            google.maps.event.addDomListener(window, 'load', initialize);
        </script>
    </head>
    <body>
        <div id="result">
            <?php
                print_r($_POST);

                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                  echo 'POSTed';
                } else {
                    echo $_SERVER['REQUEST_METHOD'];
                }
            ?>
        </div>
        <div id="map_canvas"></div>

    </body>
</html>

2 个答案:

答案 0 :(得分:2)

为您的数据添加密钥?

 data: {'lat':lat}

答案 1 :(得分:1)

尝试这样的事情:

var lat_value = event.latLng.lat();
var lng_value = event.latLng.lng();

 $.ajax({
        type: "POST",
        url: "index.php",
        data: {lat:lat_value},
        success: function (data) {
        console.log(data);
        }
 });
In your index.php script use:
$lat=$_POST['lat'];