简体   繁体   English

需要帮助,使用JavaScript从新HTML页面上的HTML表单返回数据

[英]Need Help Returning Data From HTML Form on New HTML Page using JavaScript

I'm having a bit of trouble with something I'm sure most of you will laugh at. 我对某些事情有些麻烦,我敢肯定你们大多数人都会笑。

I have a form built into one HTML page (contact.html), and I need to have a second HTML page (submit.html) display the information entered into the form using JavaScript. 我在一个HTML页面(contact.html)中内置了一个表单,并且需要第二个HTML页面(submit.html)显示使用JavaScript输入到表单中的信息。 Here is my form code from page 1: 这是我从第1页开始的表单代码:

<form name="contact" method="post" action="submit.html">
    <p>First Name: *</p><input type="text" name="first">
    <p>Last Name: *</p><input type="text" name="last">
    <p>Cell Phone Number:</p><input type="text" name="number">
    <p>E-mail Address: *</p><input type="text" name="address">
    <p>Comment: *</p>
    <p><textarea name="comments" cols="25" rows="5">
</textarea></p>
    <p><input type="submit" value="Submit"></p>
    </form>

Can someone please point me in the right direction? 有人可以指出正确的方向吗? I'm literally brand new to JavaScript, so I don't understand much of any of it yet. 我实际上是JavaScript的新手,所以我对其中的任何内容都不了解。

Thank you in advance, and please take it easy on me! 在此先感谢您,请放心!

You can't get post parameters using javascript - post parameters are going through the headers in a hidden way and you have to set your second page to be some server side page (php,asp.net etc...) In php for example you can get post parameters using this: 您无法使用javascript获取发布参数-发布参数以隐藏的方式通过标题,您必须将第二页设置为某些服务器端页面(php,asp.net等),例如在php中您可以使用以下命令获取发布参数:

$first_value = $_POST['first'];

If you still want to use only javascript you can change your method of the form to "get" ( method="get" ) and then the parameters will be in the URL - and then in the other page you can use a regexp to take the get params using this function: 如果您仍然只想使用javascript,则可以将表单的方法更改为“ get”( method="get" ),然后参数将位于URL中-然后在另一个页面中,您可以使用regexp使用此函数获取参数:

function getQSParam(key,target){
    var value = '';
    if(!target){
        target = location.href;
    }

    var pattern = key + '=([^&]+)';
    var o_reg = new RegExp(pattern,'i');
    var matches = o_reg.exec(target);
    if(matches && matches[1]){
        value = matches[1];
    }

    return value;
}

So if the url will be like that: submit.html?first=karl&last=marx&... you can take those parameters using the function like that: 因此,如果该网址是这样的:submit.html?first = karl&last = marx&...,则可以使用以下函数来获取这些参数:

var first_value = getQSParam('first');
var last_value = getQSParam('last');

and so on... 等等...

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

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