简体   繁体   English

如何在html中将数据从一个页面传递到另一个页面?

[英]How to pass data from one page to another page in html?

I have a requirement where I need to pass form data from one HTML page to another HTML page.我需要将表单数据从一个 HTML 页面传递到另一个 HTML 页面。 There are 5 pages in total.共有 5 页。 The user enters data in the following order:用户按以下顺序输入数据:

1st page: Name第一页:姓名

2nd page: Weight第二页:重量

3rd page: Height第三页:高度

4th page: Country of birth第四页:出生国

5th page: Show all the data entered and also send it to the database as ajax request.第五页:显示所有输入的数据,并作为ajax请求发送到数据库。

I know I can take user data in one form and multiple pages are not needed, but this requirement is unique to my project and it has to be the way mentioned above.我知道我可以以一种形式获取用户数据并且不需要多个页面,但是这个要求是我的项目所独有的,它必须是上面提到的方式。 I have tried passing data as URL parameters but after the first page, the variable gets overwritten with the second page's variable.我曾尝试将数据作为 URL 参数传递,但在第一页之后,该变量被第二页的变量覆盖。 Please Help I am new in HTML and js.请帮助我是 HTML 和 js 的新手。

Both sessionStorage and localstorage will be work. sessionStorage 和 localstorage 都可以使用。 You can choose anyone of this as per your requirement您可以根据您的要求选择其中任何一个

sessionStorage , Example: sessionStorage ,示例:

  sessionStorage.setItem('name', 'my name');
  let data = sessionStorage.getItem('name');
  sessionStorage.removeItem('name');
  sessionStorage.clear();

localStorage : Example : 本地存储:示例:

       save data: localStorage.setItem('Name', 'My name');
       get data : localStorage.getItem('Name');
       remove data from local : localStorage.removeItem('Name');

I hope this will hellpful for you我希望这对你有用

Do you have code available to show?你有可以展示的代码吗? As it stands this might not be an answerable question.就目前而言,这可能不是一个可以回答的问题。 However, generally you can store the value from one page in an <input type="hidden"> html tag and use that value in the next call.但是,通常您可以将页面中的值存储在<input type="hidden"> html 标记中,并在下一次调用中使用该值。 Finally, you can make an ajax request via XMLHttpRequest() in your js, but you'll need a backend script like PHP or Python to handle the communication with the database.最后,您可以通过 js 中的XMLHttpRequest()发出 ajax 请求,但是您需要像 PHP 或 Python 这样的后端脚本来处理与数据库的通信。 Here's an example that I might try, assuming that you sent form data to the hypothetical page below:这是我可能会尝试的示例,假设您将表单数据发送到下面的假设页面:

<form action="height.html" method="GET">
    <input hidden="text" name="valuetopass" />
    <input type="submit" value="Submit" />
</form>

Then you can add javascript like this:然后你可以像这样添加javascript:

var data = window.location.search; // You have parse after this point

data will now contain a string containing your url appended with the value from the previous form, and you'll need to parse it to get just the value, which you can then use to set your next hidden input value with a call to document.getElementsByName("valuetopasss");数据现在中将包含您的网址与以前的形式值追加一个字符串,你需要分析它得到公正的价值,然后你就可以使用同一个呼叫设置你的下一个隐藏的输入值document.getElementsByName("valuetopasss");

HTML it stateless by nature, meaning every file access to a page is a new world. HTML 本质上是无状态的,这意味着对页面的每个文件访问都是一个新世界。 To pass data, you could encode the data in the URL or you need a backend that stores the data between the pages.要传递数据,您可以对 URL 中的数据进行编码,或者您需要一个在页面之间存储数据的后端。

Alternative approach is some browser session storage.另一种方法是一些浏览器会话存储。 If you are talking about a single page application, you can use JavaScript with an object holding the information.如果您谈论的是单页应用程序,您可以将 JavaScript 与保存信息的对象一起使用。

you can have a look at this answer你可以看看这个答案

is describes how to use either session storage or local storage or URL params描述了如何使用会话存储或本地存储或 URL 参数

but I really recommend u to use one form and to build something like a wizard form like this links但我真的建议你使用一种形式并构建像这个链接这样的向导形式

https://bootsnipp.com/snippets/eN4qy https://bootsnipp.com/snippets/eN4qy

https://www.w3schools.com/howto/howto_js_form_steps.asp https://www.w3schools.com/howto/howto_js_form_steps.asp

https://colorlib.com/wp/free-bootstrap-wizards/ https://colorlib.com/wp/free-bootstrap-wizards/

You need to use session Storage.您需要使用会话存储。 Store your variables in session storage like this.像这样将变量存储在会话存储中。

this is your html...这是你的html...

<input type="text" id="name" >
<button onClick="SaveAndGo()">Next Page</button>

in your js use this code在您的 js 中使用此代码

<script>
function SaveAndGo()
{
var name = document.egtElementById('name').value;
 sessionStorage.setItem("name",name); //the second name is the variable name...    

}
</script>

use same code for all other attributes of user like weight, height and country对用户的所有其他属性(如体重、身高和国家/地区)使用相同的代码

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM