Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

how to write hidden input with the value of my location with javascript?

Dear Experts,
I need to post the location ( lat and lng) variable to another page to save it.
how can I write the variable inside hidden input?
I use HTML5 and PHP.  My code is below

Thank you


<script>
      // Note: This example requires that you consent to location sharing when
      // prompted by your browser. If you see the error "The Geolocation service
      // failed.", it means you probably did not give permission for the browser to
      // locate you.
      var map, infoWindow;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 6
        });
        infoWindow = new google.maps.InfoWindow;

        // Try HTML5 geolocation.
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
              lat: position.coords.latitude,
              lng: position.coords.longitude
            };

            infoWindow.setPosition(pos);
            infoWindow.setContent('Location found.');
            infoWindow.open(map);
            map.setCenter(pos);
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }
      }

      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
        infoWindow.open(map);
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>

Open in new window

Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

I am pretty sure, there are ways to write a html code with a place holder for a variable.

Given below is a classic ASP code which does this. In classic ASP <% and %> are place holders which classic ASP engine will process. Var is the variable declared in classic ASP so that is server side.

<input type="hidden" value="<%=var%>">

I am pretty certain that php has similar thing too.

if you want something client side, may be you can do something like this:

html:
<input type="hidden" value="" id="hiddenVariable">

js
var hv = document.getElementById("hiddenVariable");
hv.value = jsVariable;

Place it in some js function and call function in a script tag,
Avatar of BR

ASKER

Dear Nitin,
What I'm asking is

how can I assign the variable of lat and lng variable into hidden input with javascript?

I wonder how do you do it with javascript not with php or asp?
Where and when are you getting the lat/long variable from that you need to save??
Avatar of BR

ASKER

Dear Chris Stanyon,

I want to get the location of my user and post it to the another page.
I thought, it would be the easy way to post the lat and lng variable with hidden inputs.

I don't know jquery,
but I thought lat and lng variable have the latitude and longitude of the user?

I want to learn geo location of my employee and save it to my database.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

you are great Chris Stanyon,
thank you so much
You're welcome :)