我的HTML脚本收集纬度和经度,并通过ajax将它们发送到另一个PHP脚本,后者将这些值插入到mysql中,问题是AJAX将NULL值插入到表中。互联网指出,对于gps坐标的最佳做法,它在数据库中为纬度和经度,纬度和经度插入的值均为“ 0.000000”,类型为float(10,6)。我显然是新开发人员,请帮忙-HTML脚本
<!DOCTYPE html>
<html>
<head>
<title>AJAX DATABASE</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
/*/ Wait for Cordova to load
/*/
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
// Cordova is ready
//
function onDeviceReady() {
// Throw an error if no update is received every 30 seconds
var options = {enableHighAccuracy:true, timeout: 3000, maximumAge:0};
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}
// onSuccess Geolocation
//
function onSuccess(position) {
var latitude = document.getElementById("lat");
latitude.innerHTML = position.coords.latitude;
var longitude = document.getElementById("longitude");
latitude.innerHTML = position.coords.latitude;
insertDB(position.coords.latitude,position.coords.latitude);
}
// onError Callback receives a [PositionError](PositionError/positionError.html) object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Options: throw an error if no update is received every 30 seconds.
//
var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });
// Sends latitude & longitude value to php script and alerts on success
function insertDB(latitude, longitude){
var xttp = new XMLHttpRequest();
xttp.onreadystatechange = function(){
if(this.readyState == 4){
document.getElementById('longitude').innerHTML =
this.responseText;
alert("state changed by ajax")
}
}
xttp.open("GET", "ConnectTest.php?
latitude="+latitude+"&longitude="+longitude, true);
xttp.send();
}
</script>
</head>
<body>
<p id="lat"></p><br>
<p id="longitude"></p>
</body>
</html>
和PHP-
<?php
//POST VARIABLES
$one = $_GET['latitude'];
$two = $_GET['longitude'];
//ESTABLISH CONNECTION
$servername = "localhost:3306";
$user = "client";
$pass = "sonic_client";
$db_name = "sonicstrains";
$server = mysqli_connect($servername, $user, $pass, $db_name);
$sql = "SELECT * FROM 'users' WHERE 'latitude' = ".$two;
$result = mysqli_query($server, $sql);
if(!isset($_GET['latitude'])){
echo "Nothing is set";
}
if(!$server){
die("Connetion error".mysqli_connect_error());
}else{
if (!$result){
$insert = "INSERT INTO users (latitude, longitude) VALUES ('$one', '$two')";
mysqli_query($server, $insert);
echo "Coordinates Inserted";
}
}
?>
答案 0 :(得分:0)
将xttp打开并发送功能替换为以下内容:
xttp.open("POST", "ConnectTest.php, true);
xttp.send(latitude="+latitude+"&longitude="+longitude);