Save JS Data to DB

Asked

Viewed 48 times

0

I need to save a value of a JS Variable in the database, but I have no idea how to do it. I have a page that captures the user’s location , this location comes as a javascript object, and I have no idea how to save to DB. My application is being developed on top of CAKEPHP 3. Grateful.

// 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?v=3&key=YourKEY&callback=initMap"

The variable in question is the var pos;

1 answer

0

Try to fire an ajax request to a page that only saves the data in the bank. I believe this is the best way to solve the problem.

Maybe this here can help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.