dotdx80642 2012-12-31 16:37
浏览 55
已采纳

如何使用HTML表单将JavaScript值传递到另一个PHP页面?

I have spent more than 2 months trying to solve this coding problem. First of all I know JavaScript runs on the client side and PHP runs on server.

I have two PHP pages (index23.php and index24.php), both use HTML/PHP and JS.

I'm using the HTML5 geolocation API:

<body onload="getLocation()">

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
    else
    {
        x.innerHTML="Geolocation is not supported by this browser.";
    }
}

function showPosition(position)
{
    x.innerHTML= "+" + position.coords.latitude + "+" + position.coords.longitude;  
}
</script>

I can see the geocode values on my current PHP page (index23.php). Then I click an HTML form button and the browser goes to another page (index24.php).

How can I pass the JavaScript values on the first PHP page to the second PHP page using the submit button on the form?

This is my form:

<form id="searchbox" action="index24.php" method="get">
<input name="q" type="text" placeholder="Type here"/>
<input id="submit" type="submit" value="Search"></form>
  • 写回答

3条回答 默认 最新

  • duankui3838 2012-12-31 16:53
    关注

    index23.php:

    <html>
    <head>
    <script type="text/javascript">
    function getLocation(){
        var x = document.getElementById("demo");
        if (navigator.geolocation){
            navigator.geolocation.getCurrentPosition(showPosition);
        }else{
            x.innerHTML="Geolocation is not supported by this browser.";
        }
    }
    function showPosition(position){
        var x = document.getElementById("demo"),latitude=document.getElementById("latitude"),longitude=document.getElementById("longitude");
        x.innerHTML="+" + position.coords.latitude + "+" + position.coords.longitude;
        latitude.value = position.coords.latitude;
        longitude.value = position.coords.longitude;
    }
    
    </script>
    
    </head>
    <body onload="getLocation()">
    
        <p id="demo"></p>
        <form id="searchbox" action="index24.php" method="get">
        <input name="q" type="text" placeholder="Type here"/>
        <input name="latitude" id="latitude" type="hidden">
        <input name="longitude" id="longitude" type="hidden">
        <input id="submit" type="submit" value="Search"></form>
    </body></html>
    

    index24.php

    $latitude = $_GET['latitude'];
    $longitude = $_GET['longitude'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等