我花了两个多月的时间试图解决这个编码问题.首先,我知道JavaScript在客户端运行,PHP在服务器上运行.
我有两个PHP页面(index23.php和index24.php),都使用HTML / PHP和JS.
我正在使用HTML5地理位置API:
<body onl oad="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>
解决方法:
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 onl oad="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'];