Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6103895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:46:50+00:00 2026-05-23T13:46:50+00:00

Using the HTML5 Geolocation API I’ve ended up with some variables in Javascript that

  • 0

Using the HTML5 Geolocation API I’ve ended up with some variables in Javascript that I need to pass to PHP in order to continue. My code is below, how could it be achieved? I’ve tried things along the lines of $variable = <script>document.write(variable);</script>;

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<meta name="viewport" content="width=620" />
<title>test</title>
</head>
<body>
<script type="text/javascript">
//Check if browser supports W3C Geolocation API
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
    }
else    {
    document.write("Geolocation is required for this page.");
    }
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    }
function errorFunction(position) {
    document.write("Error");
    }
</script>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$lat = //TAKE FROM JAVASCRIPT
$lng = //TAKE FROM JAVASCRIPT
$url = "http://api.geonames.org/findNearbyPlaceNameJSON?lat='.$lat.'&lng='.$lng.'&username=demo";
$json = file_get_contents($url);
$data = json_decode($json, true);
$geonames = $data['geonames'][0];
$town = $geonames['name'];
echo "Displaying results near ".$town.". <a href=#>Not in ".$town."?</a>";
?>
</body>
</html>

EDIT: OK, I’ve done some homework and now I know I’m looking at an AJAX XMLHttpRequest to slick over the two. However, its syntax has slightly thrown me (not to mention the cross-browser issues). Can anyone give me a nudge in the right direction with this one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:46:51+00:00Added an answer on May 23, 2026 at 1:46 pm

    You are mixing Server-side scripting along with browser-side scripting

    $variable dissapears on the browser-side and so it will always hold “document.write(variable);” whatever the script inside there is written in.

    You can however use AJAX to send the data back to php & process it there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to detect HTML5 Geolocation support using PHP so that I can load
I'm using HTML5 geolocation to collect the users lat / long and I need
I am using the HTML5 Geolocation API to find the longitude and the latitude
I am using the HTML5 geolocation API. I recently decided to shift from polling
Newbie question. I'm trying to make a mobile site using html5's geolocation (I know
Is there a way to make HTML5's Geolocation only work, if it is using
I followed the HTML5 Websocket tutorial of the website below: http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/ It worked well,
I'm building an iOS centric web page using HTML5, CSS3 and Javascript with jQuery
I using HTML5 D&D. I have one parent drop area, e.g. parent. In that
I want to understand how to implement HTML5 <audio> fallback using Javascript... i.e. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.