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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵