Php 使用Javascript设置表单隐藏值

Php 使用Javascript设置表单隐藏值,php,javascript,html,geolocation,Php,Javascript,Html,Geolocation,我在页面加载时调用此函数getLocation(): <script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition,showError); } } function showPosition(position) { var latlon=position.coords

我在页面加载时调用此函数
getLocation()

<script>
function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
}
function showPosition(position)
{
    var latlon=position.coords.latitude+","+position.coords.longitude;
    //alert("Your location: "+latlon);
    document.getElementById("latitude").value = coords.latitude;
    document.getElementById("longitude").value = coords.longitude;
}
function showError(error)
{

}

函数getLocation()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
}
功能显示位置(位置)
{
var latlon=位置.坐标.纬度+“,”+位置.坐标.经度;
//警报(“您的位置:+latlon”);
document.getElementById(“纬度”).value=coords.latitude;
document.getElementById(“经度”).value=coords.longitude;
}
功能错误(错误)
{
}

之后,我在HTML中定义了两个隐藏值:

<FORM id="myForm" action="insertar.php" method="post" enctype="multipart/form-data" data-ajax="false">
    ....Some other controls...
<input type="hidden" id="latitude" name="latitude" value="">
<input type="hidden" id="longitude" name="longitude" value="">
</FORM>

…其他一些控件。。。

在这个函数中,我使用用户的当前位置设置两个隐藏值
纬度
经度
。但是在PHP表单POST上,这两个值没有传递。怎么了?

我想这两行应该是这样的

document.getElementById("latitude").value = position.coords.latitude;
document.getElementById("longitude").value = position.coords.longitude;


我搜索了一些相关文章,发现这篇文章最相关。检查一下,让我知道

您是否检查过DOM以检查是否设置了值?