简体   繁体   English

PHP表单问题通过Google Maps Geotated Lat Long提交

[英]Issues with PHP form submit with Google Maps Geocoded Lat Long

I have a html form of mine which includes the following code. 我有一个我的HTML表单,其中包含以下代码。

<input type="text" style="opacity:0;" id="latlong" name="latlong" value="" />
<input type="submit" name="submit1" onClick="return validate();" value="Submit"/>

The Javascript's validate function looks like this. Javascript的validate函数如下所示。

<script type="text/javascript">
function validate()
{
    if($("#near_buil_id").val()==''){
        var address = "\""+$("#nearest_building").val()+","+$("#flat").val()+","+$("#near_road").val()+","+$("#landmark").val()+","+$("#street").val()+","+$("#region").val()+","+$("#city").val()+","+$("#state").val()+"\"";
        geocoder.geocode({ 'address': address }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            $("#latlong").val("POINT("+results[0].geometry.location.k+" "+results[0].geometry.location.A+")");
            //console.log($("#latlong").val());
            return true;
          }
          else{
            console.log(status);
            console.log(results);
            return false;
          }
          });
    }else{
        return false;
    }
    return true;
}
</script>

My issue is that when I submit this form, and try to take the value of $_POST['latlong'] , a null value is obtained. 我的问题是,当我提交此表单并尝试采用$_POST['latlong']的值时,将获得空值。 Also, when I put return false at the end of the function and console.log the value that I am appending in $("#latlong") , I get the expected value. 另外,当我在函数和console.log的末尾添加return false时,我将在$("#latlong")附加的值也得到了预期的值。

Can anyone help me in resolving the issue?? 谁能帮助我解决这个问题? Thanks in advance. 提前致谢。

In your submit button html you should move your code for onclick to onsubmit . 在您的html submit按钮中,您应该将onclick的代码移动到onsubmit onsubmit is run before the form is submitted whereas onclick is not, so in essence you are 'submitting' the form, with a null value in the latlong input, before your javascript code puts a value in the latlong input. onsubmit在提交表单之前运行,而onclick不在,因此从本质上讲,您是在javascript代码在latlong输入中添加值之前,使用latlong输入中的空值“提交”表单。

Note onsubmit should be a form attribute. 注意onsubmit应该是一个表单属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM