Get value in php variable from javascript callback function

when loading index.php, i want to echo the latitude and longitude from geolocation callback function.

<script type="text/javascript">
function callback(position) {
               latitude = position.coords.latitude;
               longitude = position.coords.longitude;
                 alert(latitude);
                 alert(longitude);
            }

            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(callback);
            }
</script>

i have to pass that value below anchor tag instead this ‘29.159500, 48.114219’ static value

[<a target="_blank" href="https://www.google.es/maps/dir/'<?php echo $location['lat']; ?>,<?php echo $location['lng']; ?>'/'29.159500, 48.114219'?hl=en"><img src="assets/images/google-maps.png" alt=""></a>](http://)

How can do this with ajax…I tried in many ways…Not getting any result.Anyone please help…

Welcome to the forums, @ambilyovt.

When you post code on the forums, you need to format it so it will display correctly.

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

(I’ve edited your post above for you.)

Where does the value of $location['lat'] come from? Is it coming from the JavaScript function callback? If it is, surely you’d do that in your JavaScript via the DOM, just by modifying the target of the href?

The only way I can see this being done in PHP is if your PHP code (running on the server) actually gets the location information, rather than the JavaScript function.

This is the store location that is stored in backend. i need current location lati and longi for mapping the route on google map from current location to store location

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.