将表单数据传递给另一个HTML页面 [英] passing form data to another HTML page

查看:1693
本文介绍了将表单数据传递给另一个HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个HTML页面:form.html和display.html。在form.html中,有一个表单:

 < form action =display.html> 
< input type =textname =serialNumber/>
< input type =submitvalue =Submit/>
< / form>

表单数据被发送到display.html。我想在display.html中显示和使用表单数据 serialNumber ,如下所示:

 <身体GT; 
< div id =write>
< p>序列号是:< / p>
< / div>
< script>
function show(){
document.getElementById(write)。innerHTML = serialNumber;
}
< / script>
< / body>

那么如何传递 serialNumber 变量从form.html到display.html,以便display.html中的上述代码将显示序列号,并且JavaScript函数show()从第一个HTML中获取 serialNumber 。如果你没有选择使用服务器端编程,比如PHP,你可以使用查询字符串或者GET参数。

在表单中,添加一个 method =GET属性:

 < form action =display.htmlmethod =GET> 
< input type =textname =serialNumber/>
< input type =submitvalue =Submit/>
< / form>

当他们提交这个表单时,用户将被引导到一个地址,其中包含 serialNumber 值作为参数。例如:

  http://www.example.com/display.html?serialNumber=XYZ 
code>

然后,您应该能够解析查询字符串 - 它将包含 serialNumber 参数值 - 来自JavaScript,使用 window.location.search 值:

  // from display.html 
document.getElementById(write)。innerHTML = window.location.search; //你将必须解析
//查询字符串来提取你需要的
//参数

另见 JavaScript查询字符串






另一种方法是在表单提交时将值存储在cookie中,并在 display.html $ b

另请参阅如何使用JavaScript填写另一页上的表单


I have two HTML pages: form.html and display.html. In form.html, there is a form:

<form action="display.html">
    <input type="text" name="serialNumber" />
    <input type="submit" value="Submit" />
</form>

The form data is sent to display.html. I want to display and use the form data serialNumber in display.html, as shown below:

<body>
    <div id="write">
        <p>The serial number is: </p>
    </div>
    <script>
    function show() {
        document.getElementById("write").innerHTML = serialNumber;
    }
    </script>
</body>

So how can I pass the serialNumber variable from form.html to display.html so that the above code in display.html will show the serial number and the JavaScript function show() gets the serialNumber from the first HTML?

解决方案

If you have no option to use server-side programming, such as PHP, you could use the query string, or GET parameters.

In the form, add a method="GET" attribute:

<form action="display.html" method="GET">
    <input type="text" name="serialNumber" />
    <input type="submit" value="Submit" />
</form>

When they submit this form, the user will be directed to an address which includes the serialNumber value as a parameter. Something like:

http://www.example.com/display.html?serialNumber=XYZ

You should then be able to parse the query string - which will contain the serialNumber parameter value - from JavaScript, using the window.location.search value:

// from display.html
document.getElementById("write").innerHTML = window.location.search; // you will have to parse
                                                                     // the query string to extract the
                                                                     // parameter you need

See also JavaScript query string.


The alternative is to store the values in cookies when the form is submit and read them out of the cookies again once the display.html page loads.

See also How to use JavaScript to fill a form on another page.

这篇关于将表单数据传递给另一个HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆