How can I separate this javascript string into two php variables?

Asked

Viewed 142 times

0

Follows the code____________________________________

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Map On</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Localizacao: " + position.coords.latitude+","+ position.coords.longitude;


}
</script>

<?php 
  $teste = "<script>getLocation()</script>";
  echo $teste;
  $latAqui = explode(" ", $teste);
  print_r($latAqui);
?>

Retorno até o presente momento_______________________________________

Localizacao: -22.3588937,-47.3859919

Array ( [0] => )

2 answers

0

You will only get through this data via Ajax.

When PHP renders the page it literally writes that $teste = "<script>getLocation()</script>";. For him "<script>getLocation()</script>" is simply a string. It has no meaning and does not execute anything.

Whoever runs Javascript is the browser after the pure HTML generated by PHP is ready and is rendered to the end user. Right now PHP has no more power over the page.

What you can do is create a function that runs this command and send the result via AJAX to your PHP project.

AJAX is separated into two steps. Sending the browser made with Javascript you can read more about in the stackoverflow itself: Ajax request with pure Javascript (no Apis)

And the API in PHP that will receive this request. Here’s a short tutorial talking about it too: http://www.devmedia.com.br/criando-um-cadastro-com-php-ajax-e-jquery/28046

It’s not a trivial process. You will need to study a little to understand how to accomplish even because they are asynchronous calls, but it is the only way to get the user’s location through the Browser API.

  • Could you give an example or a website for me to see, please?

  • @Julioramos complemented the answer. Understand that it is not a trivial process. You can’t put the complete code of the project here because you have to tinker with several tips, but it’s the only way to do this check. Without relying on browser integration only if done by IP. But you will need to consult an external service or buy a geolocated IP base.

  • By ip is not possible, it returns ips from the region, because ip is dynamic.

  • The IP base takes the region prefix. It is usually accurate at city level. Even though the user has a dynamic IP, they can get it right because it picks up the location prefix. Using the browser’s geolocation API only passing through AJAX itself. You can pass data from PHP to the browser directly on the page, but you can never get past Javascript pro PHP without another connection like AJAX and Websockets for example.

-1

First remove the location of the function and leave so:

return position.coords.latitude+","+ position.coords.longitude;

Afterward:

 $teste = "<script>getLocation()</script>";
 echo $teste;
 $latAqui = explode(",", $teste);
 echo($latAqui[0]);
 echo($latAqui[1]);
  • I get the following error : Notice: Undefined offset: 1 in C: xampp htdocs testeGEOIP.php on line 35

  • Which is line 35?

  • This one: print_r($latAqui[1]);

  • I edited the answer

  • You’re still wrong, buddy ^-^

  • From a print_r($latAqui). What is the return?

  • Array ( [0] => )

  • and in echo($test), which return?

  • -22.3588937,-47.3859919

  • Minto, the Function result is null

  • I redacted the answer

  • And remember. The variable test needs to have content

  • 1

    @Lucastorres the value he expects is the return of the function getLocation. Since this script only runs in the browser it ends up taking literally the string that is set in PHP that has no value for it. It won’t work that way.

Show 8 more comments

Browser other questions tagged

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