简体   繁体   English

将JavaScript元素ID传递到PHP或表单变量中

[英]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. 我正在使用google maps API根据人员地理位置的纬度和经度来获取城市。 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. 我在网上找到了运行许多此类代码的出色代码,并且下面的代码可以在页面上显示城市名称,但是我不知道如何将其转换为PHP变量。 I'm not very fluid with Javascript so any help would be awesome. 我不太喜欢Javascript,因此任何帮助都很棒。 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: 这是我的代码,如何将“ geo” ID获取到PHP变量中,甚至隐藏在我可以发布到另一页的隐藏表单字段中:

<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. 它几乎与您已经拥有的完全相同,只需以相同的方式将值插入Javascript的隐藏字段中,然后像您所说的那样将其发布即可。 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="" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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