我不知道为什么我的code不工作,我想从JavaScript发送的坐标使用AJAX PHP和我能够从JavaScript中提取值到文本框,但值不会传递到PHP。任何帮助是非常AP preciated。
I don't know why my code is not working, I am trying to send the coordinates from JavaScript to PHP using AJAX and I am able to pull values from JavaScript to textbox but values are not passed to PHP. Any help is highly appreciated.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
$( document ).ready(function() {
$.ajax({url:"samepage3.php",type:"POST",async:false,
data:{getlat:$("#getlat").val(),getlon:$("#getlon").val()}
});
});
</script>
</head>
<body onload="getLocation()">
<input type="text" id="getlat" name="getlat" />
<input type="text" id="getlon" name="getlon" />
<?php
if(isset($_POST["getlat"]))
{
$lat = $_POST["getlat"];
echo $lat;
}
if(isset($_POST["getlon"]))
{
$lon = $_POST["getlon"];
echo $lon;
}
?>
</body>
</html>
更新1:
我在运行此code文件samepage3.php,我需要的动作来发生在同一页上无需重新加载页面
您无法在samepage3.php执行PHP脚本,在的页面完全加载。我建议单独的页面,然后创建一个使用AJAX的响应。事情是这样的。
You can not execute the PHP script on the samepage3.php, after the page is fully loaded. I suggest to separate the page, and create the response using AJAX. Something like this.
index.php文件
File index.php
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
function fireAjax(){
$.ajax({url:"response.php",type:"POST",async:false,
data:{getlat:$("#getlat").val(),getlon:$("#getlon").val()},
success: function (response){
$('#response').html(response);
}
});
}
</script>
</head>
<body onload="getLocation()">
<input type="text" id="getlat" name="getlat" />
<input type="text" id="getlon" name="getlon" />
<button onclick="fireAjax()" >Send</button>
<div id="response">
</div>
</body>
</html>
文件response.php
File response.php
<?php
if (isset($_POST["getlat"]))
{
$lat = $_POST["getlat"];
echo 'Latitude: ', $lat, '<br/>';
}
if (isset($_POST["getlon"]))
{
$lon = $_POST["getlon"];
echo 'Longitude : ', $lon;
}
上一篇:从Ajax调用中传递一个变量的函数变量、函数、Ajax
下一篇:使用GridView控件内面板的Ajax TabContainer的,一个按钮的TabContainer的外控件、按钮、面板、GridView