如何获得document.getElementById的结果

时间:2018-07-22 11:29:48

标签: javascript php jquery

这是我的Javascript

              <script type="text/javascript">


                window.onload=function(){

            /* This showResult function is used as the callback function*/

            function showResult(result) {
                document.getElementById('latitude').value = result.geometry.location.lat();
                document.getElementById('longitude').value = result.geometry.location.lng();
            }

            function getLatitudeLongitude(callback, address) {
                // If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
                address = address || 'Ferrol, Galicia, Spain';
                // Initialize the Geocoder
                geocoder = new google.maps.Geocoder();
                if (geocoder) {
                    geocoder.geocode({
                        'address': address
                    }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            callback(results[0]);
                        }
                    });
                }
            }

            var button = document.getElementById('btn');

            button.addEventListener("click", function () {
                var address = document.getElementById('address').value;
                getLatitudeLongitude(showResult, address)
            });

                }

            </script>

这是我的html代码

            <input id="address" type="text" size="50" name="city" class="form-control" placeholder="Изберете локация" autocomplete="on">

                <button  id="btn" name="get" value="get">get</button>
                <div>


            The result of latidude and longitude id to be added as value

                    <p>Latitude:
                        <input type="text" id="latitude" name="latitude" readonly />
                    </p>
                    <p>Longitude:
                        <input type="text" id="longitude" name="longitude" readonly />
                    </p>

结果在输入时按ID显示,但我无法获取变量

如何获取document.getElementById 纬度经度的结果,并转换为用于mysql插入的php变量

1 个答案:

答案 0 :(得分:0)

下面的JavaScript函数可以帮助您向服务器上的POST文件发送save_coordinates.php请求,以将坐标值即latitudelongitude保存在数据库中。

function saveLatLong(latitude, longitude){
   var xhttp = new XMLHttpRequest();
   xhttp.open("POST", "save_coordinates.php", true);
   xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xhttp.send("latitude="+latitude+"&longitude="+longitude);
   return true;
}