我正在编写一段能够找到用户的LATITUDE和LONGITUDE的代码,并且会在点击按钮时通过PHP保存MySQL数据库。它给了我弹出窗口的坐标,但没有将它保存到数据库。这里'我的代码 -
我相信我在edit.php上的客户端不正确。
<?php
include_once("php_includes/database.php");
// If not logged in - Redirect to login
if( $log_user == ""){
header("location: index.php");
exit();
}
?>
<?php
$sql = "SELECT * FROM users WHERE user='$log_user' LIMIT 1";
$result = $database->query($sql);
while($record = $database->fetch_array($result)){
$fn = $record['fname'];
$ln = $record['lname'];
$latlon = $record['latlon'];
}
?>
<?php
if(isset($_POST['submit'])){
$fname = trim($database->escape_value(ucfirst(preg_replace('# [^a-zA-Z ]#i', '',$_POST['fname']))));
$lname = trim($database->escape_value(ucfirst(preg_replace('#[^a-zA-Z ]#i', '',$_POST['lname']))));
$latlon = trim($database->escape_value(preg_replace('#[^a-z0- 9., ]#i', '', $_POST['latlon'])));
$sql = "UPDATE users SET
fname = '{$fname}',
lname = '{$lname}',
latlon = '{$latlon}',
WHERE user = '{$log_user}'";
$query = $database->query($sql);
if($database->affected_rows() == 1){
header("location: profile.php?u=$log_user");
exit();
}
}
}
?>
<?php
if(isset($_POST["latlon"])){// Button which updates location VIA Google Map API
// CONNECT TO THE DATABASE
include_once("php_includes/db_conn.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$latlon = trim($database->escape_value(preg_replace('#[^a-z0-9.,]#i', '', $_POST['latlon'])));
// FORM DATA ERROR HANDLING
if($latlon =="notSet"){
echo "The form submission is missing values.";
} else {
// END FORM DATA ERROR HANDLING
// Begin UPDATING Location data into the database
$sql = "UPDATE users SET
latlon = '{$latlon}',
WHERE user = '{$log_user}'";
$query = mysqli_query($db_conx, $sql);
echo "update_done";
}
}
?>
<html>
<head>
<title>Edit Profile</title>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/geo.js"> </script>
<script type="text/javascript" src="js/auto.js"></script>
<script type="text/javascript">
function locme(){
if(latlon == "notSet"){
status.innerHTML = "Please use locate button to verify your city.";
} else {
_("upbtn").style.display = "none";
status.innerHTML = 'updating...';
var ajax = ajaxObj("POST", "edit_profile.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText != "update_done"){
status.innerHTML = ajax.responseText;
_("upbtn").style.display = "block";
}
}
}
ajax.send("&latlon="+latlon);
}
}
</script>
</head>
<body>
<?php include("nav_bar_1.php"); ?>
<div class="page_index">
<h2>Edit Your Profile</h2>
<div id="status"></div>
<div id="abcd"></div>
<br/>
<form id="edit_form" action="edit_profile.php" method="post">
<h4>Name</h4>
<input type="text" name="fname" value="<?php echo htmlentities($fn); ?>" >
<input type="text" name="lname" value="<?php echo htmlentities($ln); ?>" >
<button class="upbtn" onClick="getLocation();locme();">Locate me now</button> //Button which find LONG and LAT of user when user hits.
</form>
</div>
<?php include_once("footer_bar_1.php"); ?>
</body>
</html>
这是MAP API代码
var latlon = "notSet";
function getLocation (){
navigator.geolocation.getCurrentPosition(getPos, getError);
}
function getPos(position){
latlon = position.coords.latitude+","+position.coords.longitude;
alert(latlon);
}
function getError(error){
var locError = error.code;
var latlon = "notSet";
alert(latlon);
}