dtnwm4807 2013-08-05 21:08
浏览 53
已采纳

在php提交之前使用JavaScript填写隐藏的输入

I have a submit button on a form that says this:

<input type="submit" name="add_submit" onclick="return getCoord()" value="Add Location" />

Two hidden input fields like this:

<input id="long" name="long" type="hidden"  value=""  />
<input id="lat" name="lat" type="hidden"  value="" />

The script looks like this:

<script type="text/javascript">
    function getCoord() {

        address = document.getElementById('street').value + " " + document.getElementById('city').value + " " + document.getElementById('state').value + " " + document.getElementById('zip').value;
        console.log(address);
        var geocoder = new google.maps.Geocoder();

        geocoder.geocode( { 'address': address}, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();

                document.getElementById('lat').value = latitude;
                document.getElementById('long').value = longitude;


            } 
        });

    } 
</script>

on the php post:

        $new_long = $database -> escape_value($_POST['long']);
        $new_lat = $database -> escape_value($_POST['lat']);

I want the JS function to fill out the value of the long and lat fields BEFORE the form submits; then on the php post I want to grab the input field -- but it returns blank. Is there a typo a problem with logic?

Note: I confirmed the address is being logged in the JS console correctly.

EDIT: I am actually getting a DB query fail error but it SEEMS the result of that is blank long lat entries. These coordinates are being put into a DB.

  • 写回答

1条回答 默认 最新

  • dsxgby126001 2013-08-05 21:11
    关注

    Google API maps is async, so before it has finished you have submited form. Do it on server or try with Observer.

    edit

    var geoCompleted = false;
    
    function getCoord() {
    
        address = document.getElementById('street').value + " " + document.getElementById('city').value + " " + document.getElementById('state').value + " " + document.getElementById('zip').value;
        console.log(address);
        var geocoder = new google.maps.Geocoder();
    
        geocoder.geocode( { 'address': address}, function(results, status) {
    
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
    
                document.getElementById('lat').value = latitude;
                document.getElementById('long').value = longitude;
                geoCompleted = true
                $('#form-id').submit();
    
            } 
        });
       return geoCompleted
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)