2012-12-31 59 views
3

我花了2個多月的時間試圖解決這個編碼問題。首先我知道JavaScript運行在客戶端,PHP運行在服務器上。如何使用HTML表單將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> 
+0

隱藏字段... –

回答

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']; 
+0

你將不得不切換到'$ _ POST '它的方法不顯示在URL –

+0

好的非常感謝你。 –

6

你需要做的是使一個輸入型隱藏,並通過它

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

也HTML不是服務器端。只有PHP是送超值。我希望下面的圖片解釋

enter image description here

image source

+0

像圖片:d – raqulka

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> 
相關問題