douqing5981 2014-06-11 13:40
浏览 49

使用Google Maps Geocoded Lat Long提交PHP表单的问题

I have a html form of mine which includes the following code.

<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.

<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. 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.

Can anyone help me in resolving the issue?? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • drxm72811 2014-06-11 13:55
    关注

    In your submit button html you should move your code for onclick to 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.

    Note onsubmit should be a form attribute.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法