简体   繁体   English

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

[英]How can I pass JavaScript values to another PHP page using an HTML 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. 首先,我知道JavaScript在客户端运行,PHP在服务器上运行。

I have two PHP pages (index23.php and index24.php), both use HTML/PHP and JS. 我有两个PHP页面(index23.php和index24.php),都使用HTML / PHP和JS。

I'm using the HTML5 geolocation API: 我正在使用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>

I can see the geocode values on my current PHP page (index23.php). 我可以在当前的PHP页面(index23.php)上看到地理编码值。 Then I click an HTML form button and the browser goes to another page (index24.php). 然后我单击一个HTML表单按钮,浏览器转到另一个页面(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? 如何使用表单上的提交按钮将第一个PHP页面上的JavaScript值传递到第二个PHP页面?

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>

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. 也是html不是服务器端..只有PHP是。 i hope image below explain 我希望下图解释

在此输入图像描述

image source 图像源

index23.php: 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将php变量传递给同一页面中的其他表单? - How can I pass a php variable to another form in the same page? 如何将javascript的值传递给html表单上的php? - How can I pass the value from a javascript to php on an html form? 如何使用PHP和Javascript将数据从一页传递到另一页? - How can I pass data from one page to another using PHP and Javascript? 我可以使用php将变量从一个html页面传递到另一个html页面吗? - Can I pass a variable from one html page to another html page using php? 如何不使用表单将PHP页面中的变量传递到另一个页面? - How to pass variables in PHP page to another page without using form? 导航并将值传递到PHP(HTML)的另一个页面 - Navigate and pass values to another page in PHP (HTML) 为什么我不能让HTML表单将值传递给PHP? - Why can't I get HTML form to pass values to PHP? 如何使用javascript将表单值传递到另一个文件? - How to pass form values to another file using javascript? 如何借助 php 中的选择选项将值从一个页面传递到另一个页面? - How i can pass values from one page to another with the help of select option in php? 如何在没有PHP的情况下用PHP和Javascript将数据从一页传递到另一页? - How can I pass data from one page to another in PHP and Javascript without from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM