How can I pass JavaScript values to another PHP page using an HTML form? - Stack Overflow

I have spent more than 2 months trying to solve this coding problem. First of all I know JavaScript run

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>

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>
Share Improve this question edited Jan 2, 2013 at 8:09 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Dec 31, 2012 at 16:37 Adrian DiazAdrian Diaz 271 gold badge2 silver badges6 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

what you need to do is make a input type hidden and send value through it

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

also html is not server side .. only php is. i hope image below explain

image source

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

You can add the values to the form dynamically when you get the coordinates.

<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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745228340a4617566.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信