Skip to content

Instantly share code, notes, and snippets.



Created Jun 9, 2015
What would you like to do?
read geolocation and use jquery ajax to post back
$name = $_POST["name"];
$lat = $_POST['latitude'];
$lon = $_POST['longitude'];
//TODO: Save to db
echo date('d M Y h:i:s');
<title>Tracker by &#936; [PSI]</title>
<style type="text/css">
input[type=text] { margin-bottom: 8px; display: block; clear: right; width: 150px; }
label { float: left; display: block; width: 50px; text-align: right; padding-right: 8px; }
.buttons { padding-left: 58px; margin-bottom: 8px; }
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(document).ready(function() {
$name = myCookie('name', null);
if ($name) $('#name').val($name);
function getLocation() {
if (navigator.geolocation) {
} else {
$("#latitude").val("not supported.");
$("#longitude").val("not supported.");
function showPosition(position) {
function saveLocation()
if ($('#name').val() == '')
alert('Please enter your name');
myCookie('name', $('#name').val());
url: "data.php",
type: 'POST',
data: $('form').serialize()
}).success(function(response) {
}).error(function(response) {
$('#saved').val("an error occurred");
function myCookie(key, val)
if (val != null)
document.cookie = key + "=" + val;
val = document.cookie;
if (val == null) return false;
return val.replace(key + '=', '').split('; ')[0];
<form method="post">
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
<label for="latitude">Lat:</label>
<input type="text" name="latitude" id="latitude" />
<label for="longitude">Long:</label>
<input type="text" name="longitude" id="longitude" />
<div class="buttons">
<input type="button" onclick="javascript:saveLocation();" value="save" />
<label for="saved">Saved:</label>
<input type="text" name="saved" id="saved" readonly="readonly" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.