duanmengsuo9302 2012-08-16 16:05
浏览 18
已采纳

将JavaScript元素ID传递给PHP或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>"; ?>
  • 写回答

1条回答 默认 最新

  • dongzz4545 2012-08-16 16:13
    关注

    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="" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数