3

我花了两个多月的时间试图解决这个编码问题。首先,我知道 JavaScript 在客户端运行,而 PHP 在服务器上运行。

我有两个 PHP 页面(index23.php 和 index24.php),都使用 HTML/PHP 和 JS。

我正在使用 HTML5 地理定位 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>

我可以在我当前的 PHP 页面 (index23.php) 上看到地理编码值。然后我单击一个 HTML 表单按钮,浏览器转到另一个页面 (index24.php)。

如何使用表单上的提交按钮将第一个 PHP 页面上的 JavaScript 值传递到第二个 PHP 页面?

这是我的表格:

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

3 回答 3

6

您需要做的是隐藏输入类型并通过它发送值

<input name="bla"  type="hidden" value="blabla"> 

html也不是服务器端..只有php是。我希望下面的图片能解释

在此处输入图像描述

图片来源

于 2012-12-31T16:45:50.863 回答
1

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'];
于 2012-12-31T16:53:03.037 回答
0

您可以在获取坐标时动态地将值添加到表单中。

<form id="searchbox" action="index24.php" method="get">
<input name="q" type="text" placeholder="Type here"/>
<input id="submit" type="submit" value="Search">
<input name="latitude" type="hidden" id="latitude" value="" />
<input name="longitude" type="hidden" id="longitude" value="" />
</form>
<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)
{
  document.getElementById('latitude').value = position.coords.latitude;
  document.getElementById('longitude').value = position.coords.longitude;
  x.innerHTML="+" + position.coords.latitude + "+" + position.coords.longitude;    
}
</script>
于 2012-12-31T16:49:29.850 回答