如何从另一个页面的表单中获取数据(JavaScript) [英] How to get data from a form in another page (JavaScript)

查看:154
本文介绍了如何从另一个页面的表单中获取数据(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的英语不够好,所以希望您了解我的问题...

My English is not good enough so I hope you understand what's my problem...

我有一个带有表单的页面"A".当我单击提交"时,我希望它打开/重定向到页面"B",并且要在页面"B"上显示页面"A"中的表单数据...我想使用JavaScript来做到这一点,不在jQuery左右.我尝试使用window.opener或类似的方法...接下来,显示示例代码.

I have a page 'A' with a form. 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. I've tried with window.opener or things like that... Next, the example code is shown.

页面"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> 

页面"B":

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

页面"B"可以显示页面"A"上的数据非常重要.我不能使用PHP或其他技术...:/

It's really important that page 'B' can show data from the form on page 'A'. I can't use PHP or another technology... :/

希望您能理解我的要求.谢谢!

I hope you understand what I'm asking. Thanks!

**更新

正如Sagar Hani指出的那样,解决方案(至少对我而言)是localStorage.无论如何,感谢回答的人!

The solution (at least for me) is localStorage, as Sagar Hani indicated. Anyway, thanks to people who answered!

推荐答案

  • 您永远无法从客户端(使用javascript或任何其他客户端语言)获取 POST 数据
  • 您必须使用服务器端语言来获取帖子数据(例如PHP,nodeJs).
  • 但是您可以简单地从客户端(javascript)获取 GET 数据.
    • Never you can get POST data from client side (using javascript or any other client side language)
    • You must use a server side language to get post data (like PHP, nodeJs).
    • But you can simply get GET data from client side (javascript).
    • 以下是您想要的确切解决方案.

      Following is the exact solution that you want.

      页面"A"(a.html)

      Page '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> 
      

      页面'B'(b.html)

      Page '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>
      

      这篇关于如何从另一个页面的表单中获取数据(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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