如何从另一个页面获取表单操作信息

时间:2020-06-17 14:06:10

标签: javascript php html

<form action = "AnotherPage.php" method = "get">
    <table id="uniqueID">
        <tr>
            <td><input id="box1" type="checkbox" name="box1" value = "apple"/><label
                    for="box1">apples</label></td>
        </tr>

    </table>
    <input type="submit" value="Submit">

</form>

因此,当用户提交它时,我将如何在anotherPage.html中获取box1 = apple信息。最好是谢谢没有jQuery。因此,我想让anotherPage.html文件执行console.log(框1 = apple)并在anotherPage的html文件中。 “盒子1 =苹果”(当然不需要对其进行硬编码)

编辑。因此,我认为使用php更好。我将如何在AnotherPage.php中回显此数据

1 个答案:

答案 0 :(得分:0)

如果要在没有任何后端服务器的情况下使用纯HTML和JS,则可以使用GET方法将表单数据作为URI查询参数发送到下一页:

<form action = "anotherPage.html" method = "get">
    <table id="uniqueID">
        <tr>
            <td><input id="box1" type="checkbox" name="box1" value = "apple"/><label
                    for="box1">apples</label></td>
        </tr>

    </table>
    <input type="submit" value="Submit">
</form>

然后从下一页的URI查询中解析它:

<html>
  <body>
  <script>
    const urlParams = new URLSearchParams(window.location.search);
    const apples = urlParams.get('box1');
    console.log(apples);
  </script>
  </body>
</html>