0

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>
  • 1
    http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt – Nouphal.M Jan 04 '14 at 04:48

1 Answers1

0

As far as I can tell the problem is just some javascript errors.

Starting with <body BGCOLOR="#FFFFFF" onLoad="writeLati(locform)">

locform doesn't exist. You need to use something like document.getElementById to get what you are expecting here. (To use getElementById you need to put an ID on your form obviously).

Also in function getPosition(position)

You are trying to assign form.latitude.value, however form is a local variable in the function writeLati it cannot be accessed here.

It worked for me after fixing those problems.

  • Sorry to bother you with this, but Im not a programmer (well I used to be 30 years ago when languages and logic were totally defferent). Would you please correct my code with your suggestions and post it? – user3159437 Jan 05 '14 at 03:02