douxian5963 2014-04-05 09:48
浏览 152
已采纳

始终清空$ _POST

I'm trying to save a Google Maps Marker's coordinates to my database after the user placed the Marker. When I send the coordinates to the server, I always receive an empty $_POST array. Where did I go wrong?

Here's my 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条回答 默认 最新

  • dqluw20882 2014-04-05 09:59
    关注

    Try something like this:

    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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数