简体   繁体   English

将JavaScript变量发送到要与表单一起提交的另一页

[英]Send a JavaScript Variable to another page to be submitted with a form

Let me explain.... I am trying to have the user confirm their location on one page using Google Maps v3 API. 让我解释一下。...我试图让用户使用Google Maps v3 API在一页上确认其位置。 I want to save and send the position variable (the latitude and longitude) of the user to the next page to be submitted with a form the user fills out. 我想保存用户的位置变量(纬度和经度),然后将其发送到下一页,并与用户填写的表单一起提交。 In the end the form is submitted to a database. 最后,将表单提交到数据库。

I believe the variable I am wanting to save is the following: 我相信我要保存的变量如下:

var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

The script is in the Site.Master in the heading. 该脚本位于标题的Site.Master中。

Would creating a form and sending it to the next page be the easiest way to accomplish this? 创建表单并将其发送到下一页是实现此目的的最简单方法吗?

Google Maps Page: Google地图页面:

<%@ Page Title="Roadside Assistance: Location" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="RoadsideLoc.aspx.cs"     Inherits="Final_Project.RoadsideLoc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeaderContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div style="text-align:center">
    <h1>Is this the location of your vehicle?</h1>

</div>

<div id="map-canvas" style="height:350px">

</div>

<div data-role="controlgroup" data-type="horizontal" style="text-align:center" runat="server">
    <a href="Roadside.aspx?" data-role="button">Yes</a>
    <a href="RoadsideLocNO.aspx" data-role="button">No</a>

</div>


</asp:Content>

You could use localStorage or sessionStorage to store lat/lng and then recreate a LatLng object when you need it. 您可以使用localStoragesessionStorage存储lat / lng,然后在需要时重新创建LatLng对象。

Alternatively you can use cookies to store you data on client side. 另外,您可以使用Cookie在客户端存储数据。

You can accomplish this by applying several methods. 您可以通过应用几种方法来完成此操作。

  1. After the user confirms their location, do something like 用户确认其位置后,请执行以下操作

     var lat = position.coords.latitude, long = position.coords.longitude; window.location = "sitemaster2.aspx?latitude=" + lat + "&longitude=" + long; 

    This way, you will be able to access this coordinates in the second page and attach them to the desired form. 这样,您将能够在第二页中访问此坐标并将其附加到所需的表单。

  2. Right before leaving the page append the current URL the coordinates. 在离开页面之前,将当前URL附加到坐标之后。 You can do that with history.pushState . 您可以使用history.pushState做到这一点。 When you get to the form, search the history for your entry and get the coordinates out of the URL. 到达表单后,请在历史记录中搜索条目,并从URL中获取坐标。

  3. Set a cookie with the coords and get them back on the second page. 设置带有坐标的cookie,然后将它们返回第二页。

  4. Asynchronously load an asp file which sets a session holding the coords. 异步加载一个asp文件,该文件设置一个保存坐标的会话。 Then get them by checking the sessions. 然后通过检查会话获取它们。

  5. Put the coords in a hidden POST form and make the user actually submit the form when pressing the confirm button. 将坐标置于隐藏的POST表单中,并在按下确认按钮时使用户实际提交表单。

Or don't change the pages at all. 或根本不更改页面。 You can just have both pages in one. 您可以将两个页面合而为一。 Split the body in two and alternatively show each part as a step to submitting the necessary data. 将正文分为两部分,或者显示每个部分,作为提交必要数据的步骤。

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

相关问题 使用提交按钮提交表单时,如何将javascript变量发送到php中的另一页 - How to send javascript variable to another page in php when form is submitted using submit button 提交表单时,将 javascript 变量传递到另一个页面。 仅使用 html 和 Javascript - Pass a javascript variable to another page when form is submitted. with just html and Javascript Reactjs:如何在提交时路由到另一个表单并将提交的数据发送到新页面 - Reactjs : How to route to another form on submit and send submitted data to the new page 防止在提交表单之前转到另一页 - Prevent to go another page until form is submitted 检查表单是否已提交,并使用if语句更改javascript变量 - Check if form submitted and Change javascript variable with if statement 提交表单后重定向页面-Javascript - Redirect the Page after Form is Submitted - Javascript 提交表单到javascript并收到结果后,如果不重新加载页面,则无法提交另一个值 - After submitting a form to javascript and receiving result , another value cannot be submitted without reloading the page 在同一页面提交表单时如何打开另一页面 - how to open another page when form submitted at the same page 提交表单时在另一页上生成弹出窗口? - generating pop up on another page when form is submitted? 表格未使用javascript提交 - Form not being submitted with javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM