未设置表单数据


Form data not getting set

这是我的HTML代码:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">           
        </script>
        <script>
            function getCords(){
                var x=document.forms["get_loc"]["loc"].value;
                var y=x.replace(" ","+");
                $.getJSON("http://maps.google.com/maps/geo?q=%22"+y+"%22&key=%22My_Google_API_Key%22&sensor=false&output=json",
                    function(data, textStatus){
                        //alert(data.Placemark[0].Point.coordinates[0]);
                        //document.getElementById("MyDiv").innerHTML=data.Placemark[0].Point.coordinates[0] 
                        $("#lon").val(data.Placemark[0].Point.coordinates[1]);
                        $("#lat").val(data.Placemark[0].Point.coordinates[0]);
                    console.log(data);
                });
            return true;            
            }
        </script>
    </head> 
    <body>
        <form name="get_loc" id="get_loc" action="get_loc.php" onsubmit="return getCords()" method="post">
        Location:   <input type="text" name="loc" id="loc"/></br>
        <input type="hidden" name="lat" id="lat" value="0"/>
        <input type="hidden" name="lon" id="lon" value="0"/>
        <input type="submit" value="Submit" id="submit_button"/></br>
        </form>
    </body> 
</html> 

现在,我要做的是使用这个函数获取一个位置的坐标,并将其传递到一个单独的PHP文件中进行一些数据库操作。但是,当我尝试在PHP文件中打印值时,纬度和经度仍然显示为0。

这里出了什么问题?

P: S:当返回false并检查警报时,它会给我正确的纬度。所以这不是谷歌API的错误。

以这种方式提交表单的问题是:您在提交表单的确切时刻调用了一个javascript函数。这不会取消实际的表单提交,javascript函数(Async)也没有时间设置字段。在异步请求完成之前,页面已经离开很久了。

幸运的是,你可以取消原始事件,在得到api的结果后,提交表单。不要忘记这可能需要一些时间,并且用户不知道发生了什么,通过显示某种"加载器"让他们知道事情正在发生。

此外,通过使用jQuery-submit事件将html与javascript分离。这是如何工作的:

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">           
    </script>
    <script>
        function getCords(event){
            event.preventDefault();
            var callingForm = $(this);                
            var location = callingForm.find("#loc").val();
                location = location.replace(" ","+");
            $.getJSON("http://maps.google.com/maps/geo?q=%22"+y+"%22&key=%22My_Google_API_Key%22&sensor=false&output=json",
                function(data, textStatus){
                    //alert(data.Placemark[0].Point.coordinates[0]);
                    //document.getElementById("MyDiv").innerHTML=data.Placemark[0].Point.coordinates[0] 
                    $("#lon").val(data.Placemark[0].Point.coordinates[1]);
                    $("#lat").val(data.Placemark[0].Point.coordinates[0]);
                console.log(data);
                callingForm.submit();
            });
        return false;            
        }
        $(".jsFormSubmit").submit(getCords);

    </script>
</head> 
<body>
    <form name="get_loc" id="get_loc" class="jsFormSubmit" action="get_loc.php" method="post">
    Location:   <input type="text" name="loc" id="loc"/></br>
    <input type="hidden" name="lat" id="lat" value="0"/>
    <input type="hidden" name="lon" id="lon" value="0"/>
    <input type="submit" value="Submit" id="submit_button"/></br>
    </form>
</body> 

有两件事几乎注定了你要做的事情。你不能像那样用ajax来搜索,跨域安全限制。而且"你的谷歌api密钥"是行不通的。客户端浏览器必须只有你,或者有自己密钥的人愿意使用它。