简体   繁体   中英

Passing a JavaScript element ID into PHP or Form Variable

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>"; ?>

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="" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM