weixin_33716557 2017-11-23 11:31 采纳率: 0%
浏览 29

数据未传递到php文件

Getting trouble sending users geolocation through AJAX into a php file.

Here the code:

                    if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(function(position) {
                            var pos = {
                                lat: position.coords.latitude,
                                lng: position.coords.longitude
                            };

                            infoWindow.setPosition(pos);
                            infoWindow.setContent('You are here');
                            map.setCenter(pos);
                            $.ajax({
                                    type: 'POST',
                                    data: pos,
                                    url: '/template-userslocation.php'
                                    });

                            },
                            function() {
                                handleLocationError(true, infoWindow, map.getCenter());
                            });                     

                   } else {
                           // Browser doesn't support Geolocation
                           handleLocationError(false, infoWindow, map.getCenter());
                   };

Have tried diferent ways to write data like: data: {pos}, data:({pos}),
data: {lat: position.coords.latitude, lng: position.coords.longitude},

And that it's the error that I get in template-userslocation.php:

Notice: Undefined index: pos in /home/.../template-userslocation.php on line 7


SOLVED:

The problem was that I've been working in Wordpress and it has it own way to handle with javascript:

here the info

  • 写回答

1条回答 默认 最新

  • 游.程 2017-11-23 11:35
    关注

    pos is the name of the variable, from PHP it will not know that, it will only see $_POST['lat'] and $_POST['lng']

    So use:

    $.ajax({
        type: 'POST',
        data: {'pos': pos},
        url: '/wp-content/themes/atripby/template-userslocation.php'
    });
    

    Then from PHP access like:

    $lat = isset($_POST['pos']['lat']) ? $_POST['pos']['lat'] : null;
    $lng = isset($_POST['pos']['lng']) ? $_POST['pos']['lng'] : null;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高
  • ¥15 single positional indexer is out-of-bounds
  • ¥15 LSTM 模型数据量需要多少?
  • ¥15 mysql数据抓包开发报表
  • ¥15 linux系统下安装office打不开
  • ¥20 rsync脚本分发错误,文件不齐全
  • ¥15 labview顺序结构与时间延时