1

I'm using the google maps API to get a city based on the latitude and longitude of the persons geo-location. I want to take the city name and be able to: display the name, use it as a variable or hidden form field. I found some great code online that runs a lot of this and I've got the code below working to display the city name on the page, however I don't know how to get this into a PHP variable. I'm not very fluid with Javascript so any help would be awesome. Here is the code I have, how do I get the "geo" ID into a PHP variable or even into a hidden form field I can post to another page:

<script language="JavaScript">function updatePage() {if (xmlHttp.readyState == 4){var response = xmlHttp.responseText; document.getElementById("geo").innerHTML = response;}} </script> <? echo "<span id=\"geo\"></span>"; ?>

1 Answer 1

1

It's almost exactly identical to what you already have, just insert the value into a hidden field with Javascript in the same way, then post it like you were saying. Example code:

function updatePage() {
    if (xmlHttp.readyState == 4){
        var response = xmlHttp.responseText; 
        document.getElementById("geo").innerHTML = response;
        document.getElementById("city_name").value = response;
    }
}

​And then you'd have the hidden input field inside a form like so:

<input type="hidden" name="city_name" id="city_name" value="" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.