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

后端 未结 2 541
盖世英雄少女心
盖世英雄少女心 2021-01-16 18:57

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 pa

相关标签:
2条回答
  • 2021-01-16 19:27
    • 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>
    
    0 讨论(0)
  • 2021-01-16 19:36

    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.

    0 讨论(0)
提交回复
热议问题