Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 尝试从地图保存坐标_Php_Jquery_Mysql_Maps - Fatal编程技术网

Php 尝试从地图保存坐标

Php 尝试从地图保存坐标,php,jquery,mysql,maps,Php,Jquery,Mysql,Maps,我通过HTML5检索坐标。但是当我试图用AJAX帖子保存坐标时,没有插入任何内容。 savespot.php确实可以工作,所以ajax帖子可能有问题吗? 还是在检索坐标之前运行ajax post <!DOCTYPE html> <html> <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> <body> <p id=

我通过HTML5检索坐标。但是当我试图用AJAX帖子保存坐标时,没有插入任何内容。 savespot.php确实可以工作,所以ajax帖子可能有问题吗? 还是在检索坐标之前运行ajax post

<!DOCTYPE html>
<html>
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <body>
        <p id="demo">Click the button to get your position:</p>
        <button onclick="getLocation()">Get your location</button>
        <div id="mapholder"></div>
        <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script>
            var x=document.getElementById("demo");
            function getLocation()
            {
                if (navigator.geolocation)
                {
                    navigator.geolocation.getCurrentPosition(showPosition,showError);
                }
                else{x.innerHTML="Geolocation is not supported by this browser.";}
            }
            function showPosition(position)
            {
                lat=position.coords.latitude;
                lon=position.coords.longitude;
                latlon=new google.maps.LatLng(lat, lon)
                mapholder=document.getElementById('mapholder')
                mapholder.style.height='250px';
                mapholder.style.width='500px';
                var myOptions={
                    center:latlon,zoom:14,
                    mapTypeId:google.maps.MapTypeId.ROADMAP,
                    mapTypeControl:false,
                    navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
                };
                var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);
                var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
            }
            function showError(error)
            {
                switch(error.code)
                {
                    case error.PERMISSION_DENIED:
                        x.innerHTML="User denied the request for Geolocation."
                        break;
                    case error.POSITION_UNAVAILABLE:
                        x.innerHTML="Location information is unavailable."
                        break;
                    case error.TIMEOUT:
                        x.innerHTML="The request to get user location timed out."
                        break;
                    case error.UNKNOWN_ERROR:
                        x.innerHTML="An unknown error occurred."
                        break;
                }
            }
        </script>
        <script>
            function ajaxFunction()
            { 
                $.ajax({ 
                    url:'savespot.php', 
                    type:'POST', 
                    data:'lat='+lat+'&lon='+lng, 
                    success:function(d){ 
                        console.log(d); 
                    }, 
                    error(w,t,f){ 
                        console.log(w+' '+t+' '+f); 
                    } 
                }); 
            }
        </script>
    </body>
</html>

单击按钮获取您的位置:

找到你的位置 var x=document.getElementById(“演示”); 函数getLocation() { if(导航器.地理位置) { navigator.geolocation.getCurrentPosition(showPosition,showError); } else{x.innerHTML=“此浏览器不支持地理位置。”;} } 功能显示位置(位置) { 纬度=位置坐标纬度; lon=位置坐标经度; latlon=新的google.maps.LatLng(lat,lon) mapholder=document.getElementById('mapholder')) mapholder.style.height='250px'; mapholder.style.width='500px'; 变异性肌肽={ 中心:拉特隆,缩放:14, mapTypeId:google.maps.mapTypeId.ROADMAP, mapTypeControl:false, navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL} }; var map=new google.maps.map(document.getElementById(“mapholder”),myOptions); var marker=new google.maps.marker({position:latlon,map:map,title:“You here!”}); } 功能错误(错误) { 开关(错误代码) { 案例错误。权限被拒绝: x、 innerHTML=“用户拒绝了地理位置请求。” 打破 案例错误。位置不可用: x、 innerHTML=“位置信息不可用。” 打破 大小写错误。超时: x、 innerHTML=“获取用户位置的请求超时。” 打破 案例错误。未知错误: x、 innerHTML=“发生未知错误。” 打破 } } 函数ajaxFunction() { $.ajax({ url:'savespot.php', 类型:'POST', 数据:'lat='+lat+'&lon='+lng, 成功:职能(d){ 控制台日志(d); }, 误差(w,t,f){ 控制台日志(w+''+t+''+f); } }); }
savespot.php

<?php

    include_once 'includes/db.php';

    //  $x = "52.364659"; 
    //  $y = "13.191007"; 
    $nick = "GPSTEST";
    $adress = "Odefinierat";

    $x = @$_POST['lat'];
    $y = @$_POST['lng'];


    $sql = 'INSERT INTO location ' .
            '(name, address, lat, lng, date) ' .
            'VALUES ("' . $nick . '", "' . $adress . '", "' . $x . '", "' . $y . '", NOW())';

    $retval = mysql_query($sql, $connection);
    if (!$retval) {
        die('Could not enter data: ' . mysql_error());
    }
    echo "Entered data successfully\n";
?>

您的AJAX设置中有一个错误

$.ajax({ 
    url:'savespot.php', 
    type:'POST', 
    data:'lat='+lat+'&lon='+lng, 
    success:function(d){ 
        console.log(d); 
    }, 
    error(w,t,f){ 
        console.log(w+' '+t+' '+f); 
    } 
});
应该给出
错误:
错误(w,t,f){}
前面

    error: error(w,t,f){ 
        console.log(w+' '+t+' '+f); 
    } 

我找不到您在哪里/如何调用
ajaxFunction
,也不知道我不能确定,但我最好的猜测是您在检索位置之前发出Ajax请求

navigator.geolocation.getCurrentPosition
是异步的,它将等待用户接受您的页面想要检索其地理位置。在它们执行之前,函数将不返回任何内容。处理这一问题的最佳方法是在调用之前,在
getCurrentPosition
的回调中发出ajax请求,这样您就可以确保自己拥有该位置


在您的情况下,这意味着您可以从
showPosition()
中调用
ajaxFunction()

$.ajax({ 
  url:'savespot.php', 
  type:'POST', 
  data:{
    lat:lat,
    lon:lng
  }
}); 

我认为变量lat和lon应该用全局范围声明,或者换句话说,您需要在showPosition()函数中声明它。

How/when is
ajaxFunction()
called?在标记中添加了jquery。下次一定要加上这个。对于解决方案,为什么不先尝试使用一个简单的表单发布,看看接收脚本是否有效?然后在控制台中检查是否有使用post值发送到savespot.php的请求。请在发布之前尝试格式化代码,因此,它是可读的,人们也可以理解你在做什么…普拉文·库马尔:代码是格式化的?谢谢你的回答。但是我不能让ajax工作。我尝试将代码放在函数getLocation()之后,但没有发布任何内容。只有脚本savespot.php可以工作。@andernicken您是否尝试过向
savespot.php
发送一个普通的HTTP帖子,并查看问题所在?您是否已将
display\u errors
设置为
1
?正常的http post现在会显示错误?无法输入数据:第1行“lng”列的数据被截断。该错误是如何显示的?我尝试将lat和lng的db值从(浮动)10,6改为10,13。但我得到的只是一个错误?对不起!我考试中的错误。(lon)HTTP POST工作。不是阿贾克斯。对不起,我不明白你的意思?