I need to pass some form fields to a Perl file and I have a little form that does the trick.
Now I'd like to add 2 fields for lattitude and longitude using geolocation, but I'm getting blanks for them. I've read it's because geolocation is asynchronous but I can't find how to solve it. I even incorporated some tips from other answers but I can't get it right.
Can anybody help me fix it?
This is my code:
<html>
<head>
<script type="text/javascript">
var lati =0;
var longi =0;
function getPosition(position)
{
lati = position.coords.latitude;
longi = position.coords.longitude;
form.latitude.value = lati;
form.longitude.value = longi;
}
function writeLati(form)
{
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition);
} else {
form.latitude.value = "3";
form.longitude.value = "5";
document.write("Your browser can't do geolocation.");
}
}
</script>
</head>
<body BGCOLOR="#FFFFFF" onLoad="writeLati(locform)">
<H1>Page Title</H1>
<FORM name="locform" ACTION=../cgi-bin/rally.pl METHOD=POST>
Car Number:
<INPUT TYPE="TEXT" NAME="Number" LENGTH=2 />
<INPUT TYPE="HIDDEN" NAME="Stage" VALUE=0 />
<input type="HIDDEN" name="latitude" />
<input type="HIDDEN" name="longitude"/>
<P>
<INPUT TYPE=SUBMIT VALUE="Send">
</FORM>
</body>