简体   繁体   English

如何从另一个页面的表单中获取数据(JavaScript)

[英]How to get data from a form in another page (JavaScript)

My English is not good enough so I hope you understand what's my problem... 我的英语不够好,所以希望您能理解我的问题...

I have a page 'A' with a form. 我有一个带有表单的页面“ A”。 When I click Submit, I want it to open/redirect to a page 'B', and I want to show on page 'B' the data of the form in page 'A'... I want to do this with JavaScript, not with jQuery or so. 当我单击“提交”时,我希望它打开/重定向到页面“ B”,并且要在页面“ B”上显示页面“ A”中的表单数据...我想使用JavaScript来做到这一点,不在jQuery左右。 I've tried with window.opener or things like that... Next, the example code is shown. 我尝试使用window.opener或类似的东西...接下来,显示示例代码。

Page 'A': 页面“ A”:

<html>
    <head>
    </head>
    <body>
        <form method="post" name="form1" id="form1">
            <input type="text" name="field1">
            <input type="submit" name="submit" onclick="window.open('show.html');">
        </form>
    </body>
</html> 

Page 'B': 页面“ B”:

<html>
    <head>
    </head>
    <body>
        <script>
            var x = opener.document.forms["form1"]["field1"].value;
    document.write(x);
        </script>
    </body>
</html>

It's really important that page 'B' can show data from the form on page 'A'. 页面“ B”可以显示页面“ A”上的数据非常重要。 I can't use PHP or another technology... :/ 我不能使用PHP或其他技术...:/

I hope you understand what I'm asking. 我希望你能理解我的要求。 Thanks! 谢谢!

**UPDATE **更新

The solution (at least for me) is localStorage, as Sagar Hani indicated. 正如Sagar Hani指出的,解决方案(至少对我而言)是localStorage。 Anyway, thanks to people who answered! 无论如何,感谢回答的人!

  • Never you can get POST data from client side (using javascript or any other client side language) 永远都无法从客户端获取POST数据(使用javascript或任何其他客户端语言)
  • You must use a server side language to get post data (like PHP, nodeJs). 您必须使用服务器端语言来获取发布数据(例如PHP,nodeJs)。
  • But you can simply get GET data from client side (javascript). 但是您只需从客户端(javascript)获取GET数据即可。

Following is the exact solution that you want. 以下是您想要的确切解决方案。

Page 'A' (a.html) 页面“ A”(a.html)

   <html>
        <head>
        </head>
        <body>
            <form action="b.html" method="get" name="form1" id="form1">
                <input type="text" name="field1">
                <input type="submit" name="submit">
            </form>
        </body>
    </html> 

Page 'B' (b.html) 页面“ B”(b.html)

<script>

alert(findGetParameter("field1"))

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

</script>

Can't be PHP? 不能是PHP吗? With PHP you can also send the info to page B and display to user all the info. 使用PHP,您还可以将信息发送到页面B并向用户显示所有信息。 In fact, is more user friendly because it's a server side process. 实际上,由于它是服务器端的过程,因此对用户更友好。

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

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