you guys helped me here perfect my code!Im very happy about it and thank you so much all of you:)
One little more help i want to ask.
I have to double click the button to get the answer(coordinates). Why? I want to upload that to a mysql database, so double clicking could not be an option?
How could i make that the js runs before i can upload it to mysql with only one click?
Thanks in advance!
<?php
if(isset($_POST["ido"])) {
$g=3;
$longitudee="longitude$g";
$latitudee="latitude$g";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<script>
var x=document.getElementById("log");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="GPS szolgáltatás nem müködik ezen a böngészőn, kérlek értesítsd a rendszergazdát!";}
}
function showPosition(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("<?php echo $longitudee;?>").value = longitude;
document.getElementById("<?php echo $latitudee;?>").value = latitude;
}
</script>
<form action="" method="post">
<input type="submit" name="ido" value="Click" /></td>
<input type="hidden" name= "longitude" id="longitude3">
<input type= "hidden" name ="latitude" id="latitude3">
</form>
<?php
if(isset($_POST["ido"])){
echo "<script>getLocation();</script>";
$latitude=$_POST["latitude"];
$longitude=$_POST["longitude"];
print_r($_POST);
}
?>
</html>
Because the first click it adds the code to the page that calls the JavaScript function. The second click the JavaScript function ran and now you have the values set and you can read them.
Remember that JavaScript and PHP code do NOT run at the same time.
EDIT: I didnt know you have to put it into DB,
code update:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<body>
<form action="" method="post">
<input type="submit" name="ido" value="Click" />
<input type="hidden" name="longitude" id="longitude3">
<input type="hidden" name ="latitude" id="latitude3">
</form>
<span id="log"></span>
<script>
var x = document.getElementById("log");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else {
x.innerHTML = "GPS szolgáltatás nem müködik ezen a böngészőn, kérlek értesítsd a rendszergazdát!";
}
}
function showPosition(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementById("longitude3").value = longitude;
document.getElementById("latitude3").value = latitude;
}
getLocation();
</script>
<?php
if (isset($_POST["ido"])) {
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
print_r($_POST);
}
?>