【发布时间】:2015-12-06 15:48:57
【问题描述】:
我正在使用这个 JS 函数来获取访问者的地理位置并将其显示在页面上。这很好用。
function onGeoSuccess(location) {
console.log(location);
var strLocation = '<b>Latitude:</b> ' + location.coords.latitude + '<br />' +
'<b>Longitude:</b> ' + location.coords.longitude + '<br />' +
'<b>City:</b> ' + location.address.city + '<br />' +
'<b>Country:</b> ' + location.address.country + '<br />' +
'<b>Country Code:</b> ' + location.address.countryCode + '<br />' +
'<b>Region:</b> ' + location.address.region + '<br />' +
'<b>Accuracy:</b> ' + location.coords.accuracy + '<br />' +
'<br />'
document.getElementById('geofound').innerHTML = strLocation;
}
//The callback function executed when the location could not be fetched.
function onGeoError(error) {
console.log(error);
}
window.onload = function () {
//geolocator.locateByIP(onGeoSuccess, onGeoError, 2, 'map-canvas');
var html5Options = { enableHighAccuracy: true, timeout: 6000, maximumAge: 0 };
geolocator.locate(onGeoSuccess, onGeoError, true, html5Options, 'geo-details');
};
现在我想在每次调用时将此信息保存到 MySQL 数据库中。比如:
var strLocation = '$addtodb = "mysql_query('INSERT INTO gpsdb (lat, lon, city, country, code, region, accuracy) values (location.coords.latitude, location.coords.longitude, ...))"'
但我认为这不是正确的做法。直接从 JS 将信息输入数据库的正确方法是什么?我不需要像现在在 JS 中那样回显信息,我宁愿从数据库中提取它,一旦它进入那里。 JS 仅用于插入 DB。
【问题讨论】:
标签: javascript mysql insert geolocation