배경에 별도로 양식에 구현해야하는 GPS 스크립트가 있습니다.자바 스크립트에서 데이터베이스에 삽입
그러나 양식으로 구현하기 전에 데이터베이스의 "long"및 "lat"라는 두 필드에 위도와 경도를 각각 삽입하는 방법을 알아야합니다.
둘 다 div로 나눌 수 있었지만 여전히 데이터베이스에 저장하지 못했습니다.
그는 전체 GPS 페이지 코드 :
<script>
function geoFindMe() {
var output = document.getElementById("show");
if (!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
}
function success(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
var img = new Image();
img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=20&size=300x300&sensor=false";
output.appendChild(img);
};
function error() {
output.innerHTML = "Unable to retrieve your location";
};
output.innerHTML = "<p>Locating...</p>";
fillLatitudeLongitudeElements();
navigator.geolocation.getCurrentPosition(success, error);
}
function fillLatitudeLongitudeElements(){
document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;
}
function prompt(window, pref, message, callback) {
let branch = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
if (branch.getPrefType(pref) === branch.PREF_STRING) {
switch (branch.getCharPref(pref)) {
case "always":
return callback(true);
case "never":
return callback(false);
}
}
let done = false;
function remember(value, result) {
return function() {
done = true;
branch.setCharPref(pref, value);
callback(result);
}
}
let self = window.PopupNotifications.show(
window.gBrowser.selectedBrowser,
"geolocation",
message,
"geo-notification-icon",
{
label: "Share Location",
accessKey: "S",
callback: function(notification) {
done = true;
callback(true);
}
}, [
{
label: "Always Share",
accessKey: "A",
callback: remember("always", true)
},
{
label: "Never Share",
accessKey: "N",
callback: remember("never", false)
}
], {
eventCallback: function(event) {
if (event === "dismissed") {
if (!done) callback(false);
done = true;
window.PopupNotifications.remove(self);
}
},
persistWhileVisible: true
});
}
prompt(window,
"extensions.foo-addon.allowGeolocation",
"Foo Add-on wants to know your location.",
function callback(allowed) { alert(allowed); });
</script>
<?php include "../dbconnect.php";
if(isset($_POST['sub'])){
$l = $_POST['long'];
$la = $_POST['lat'];
$n = $_POST['name'];
mysql_query("INSERT INTO `gps`(`lat`, `long`, `name`) VALUES ('".$la."','".$l."','".$n."')");
echo "Done though";
}
?>
<p><button onclick="geoFindMe()">Locate</button></p>
<div id="show"></div>
<form action="#" method="post">
<fieldset>
Latitude 6:<br>
<input id="latitude" type="text" name="lat" readonly><br>
Longitude:<br>
<input id="longitude" type="text" name="long" readonly><br><br>
<input onclick="geoFindMe()" type="submit" value="Submit">
</fieldset>
</form>
가 추가 editied 했
<script>
var latitude = 0;
var longitude = 0;
function geoFindMe() {
var output = document.getElementById("show");
if (!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
return;
}
function success(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';
var img = new Image();
img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=20&size=300x300&sensor=false";
output.appendChild(img);
};
function error() {
output.innerHTML = "Unable to retrieve your location";
};
output.innerHTML = "<p>Locating...</p>";
fillLatitudeLongitudeDivs();
navigator.geolocation.getCurrentPosition(success, error);
}
function fillLatitudeLongitudeDivs(){
document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;
}
function prompt(window, pref, message, callback) {
let branch = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
if (branch.getPrefType(pref) === branch.PREF_STRING) {
switch (branch.getCharPref(pref)) {
case "always":
return callback(true);
case "never":
return callback(false);
}
}
let done = false;
function remember(value, result) {
return function() {
done = true;
branch.setCharPref(pref, value);
callback(result);
}
}
let self = window.PopupNotifications.show(
window.gBrowser.selectedBrowser,
"geolocation",
message,
"geo-notification-icon",
{
label: "Share Location",
accessKey: "S",
callback: function(notification) {
done = true;
callback(true);
}
}, [
{
label: "Always Share",
accessKey: "A",
callback: remember("always", true)
},
{
label: "Never Share",
accessKey: "N",
callback: remember("never", false)
}
], {
eventCallback: function(event) {
if (event === "dismissed") {
if (!done) callback(false);
done = true;
window.PopupNotifications.remove(self);
}
},
persistWhileVisible: true
});
}
prompt(window,
"extensions.foo-addon.allowGeolocation",
"Foo Add-on wants to know your location.",
function callback(allowed) { alert(allowed); });
</script>
<p><button onclick="geoFindMe()">Locate</button></p>
<div id="show"></div>
<?php if(isset($_POST['sub'])){ $lat = $_post['lat']; $lat = $_post['long']; echo $lat; } ?>
<form method="post" action="#">
<input id="latitude" name="lat">
<input id="latitude" name="long">
<input type="submit" name="sub" value="sub">
</form>
--- 업데이트] --- --- UPDATED--
감사합니다 사전에 :
1. 무엇을 의미합니까? 2. 사용자가 폼을 제출할 때마다'data'를'db'에 삽입해야합니까? 3. 당신의'insert' 블록은 어디에 있습니까? – Karthi
long/lat를 저장하기위한 버튼이 있습니까? 그렇다면 긴/위도를 2 개의 숨겨진 변수로 설정 한 다음 게시물에서 보내야합니다. –
Ive가 작동하지 않아 인서트 블록을 꺼 냈습니다. 임에 db에 longitutde 및 latitude를 삽입하려고했습니다. 필자는 두 개의 다른 사업부로 데이터를 추출하려고 시도했지만 db가 아니라는 것을 – user3473873