-2

I'm sure you can help me with my issue :).
I wrote a script with which I can read the current position - is in the console.
I would like to output it as PHP with echo but don't know how.

The script is as follows

<script>
    const successCallback = (position) => {
        console.log(position);
    };
    
    
    const errorCallback = (error) => {
        console.error(error);
    };

    navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
    console.log(latitude);
</script>

Thank you very much in advance

  • 2
    For this to work you need to [query](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) for a DOM element and then change its [textContent](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) – Reyno May 16 '22 at 13:16
  • 2
    Outputting as PHP does not make sense; as the first comment indicates, you *probably* want to take the latitude value and add it to the DOM (the visible page) somewhere. – Pointy May 16 '22 at 13:19
  • *"I would like to output it as PHP with echo"* - It's not clear to me what you mean. What you have is JavaScript code. Why do you want to involve PHP code? What server-side operation are you trying to invoke? – David May 16 '22 at 13:20
  • I don't know how to do this, maybe there is an other way to get the current Position Informations as php? – Rıdvan Şahin May 16 '22 at 13:20
  • If you determined the user's location in client-side JavaScript, and now want to work with these values in PHP - then you need to _send_ them to the server first. – CBroe May 16 '22 at 13:23
  • I need the longitude and latitude as information in a php variable - so that I can process it further. I need them to be able to measure distances between locations with further queries. – Rıdvan Şahin May 16 '22 at 13:23
  • 1
    @RıdvanŞahin: If you need to send data to the server then your options generally include an AJAX request, a form POST, or query string parameters on a link or redirect. If the goal is to keep the user on the same page, then AJAX would be what you're looking for. But it's not really clear from the question if that is your goal. The only stated goal is to "send data to PHP", which involves making a request of some kind to the server. – David May 16 '22 at 13:25
  • @CBroe and how I can send them to the server? do you have any instructions for me? I have almost no knowledge of JavaScript – Rıdvan Şahin May 16 '22 at 13:25
  • Make an AJAX or fetch request, or append them as GET parameters to a URL you then navigate to via script, or put them into hidden fields of a form that you then submit via JS ... – CBroe May 16 '22 at 13:27

0 Answers0