简体   繁体   中英

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. 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.

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

<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'. I can't use PHP or another technology... :/

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

**UPDATE

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

  • 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.

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> 

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>

Can't be PHP? With PHP you can also send the info to page B and display to user all the info. In fact, is more user friendly because it's a server side process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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