在同一个php页面上有多个表单


having multiple forms on same php page

我想创建一个php页面,其中

  1. 最初,有两个文本框和一个名为提交
  2. 单击"提交"按钮,会显示一个包含复选框和一个名为submit2的提交按钮
  3. 选中几个复选框,然后单击"提交"按钮,调用submit2,我想得到所有被选中的复选框

这是我对以上内容的尝试。步骤1和2运行良好,但是,我无法继续执行步骤3。

附言:我要求所有三个步骤都在同一页面上,每次有新的提交按钮时都不要跳转页面。此外,我使用的是GET,而不是POST。

<html>
<body>
<form action="self.php" method="get">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_GET['name']))
{
    $array1 = array(1, 2, 3, 4, 5);
    $a=count($array1);
    echo "<table width=100% border=1 cellspacing=0 cellpadding=0><tr><th>Array1</th><th>Array2</th><th>Checkboxes</th></tr>";
    for($i=0;$i<$a;$i++)
    {
        echo "<tr><td>".$array1[$i]."</td>";
        echo "<td>".$array1[$i]."</td>";
        echo '<td><input type="checkbox" name="checkbox[]" value="" id="checkbox"></td>';
        echo '</tr>';
    }
    echo "</table>";
    echo '<input type="submit" name="submit2">';
}
if(isset($_GET['submit2']))
{
    echo "pressed the second submit button";
}
?>

只需将php代码包装到类似的<form>标签中

echo '<form action="self.php" method="get">';
if(isset($_GET['name']))
{
    $array1 = array(1, 2, 3, 4, 5);
    $a=count($array1);
    echo "<table width=100% border=1 cellspacing=0 cellpadding=0><tr><th>Array1</th><th>Array2</th><th>Checkboxes</th></tr>";
    for($i=0;$i<$a;$i++)
    {
        echo "<tr><td>".$array1[$i]."</td>";
        echo "<td>".$array1[$i]."</td>";
        echo '<td><input type="checkbox" name="checkbox[]" value="" id="checkbox"></td>';
        echo '</tr>';
    }
    echo "</table>";
    echo '<input type="submit" name="submit2">';
}
echo '</form>';

此外,如果您需要从步骤1到步骤3传递nameemail变量,请添加

echo "<input type='hidden' name='name' value='{$_GET['name']}'>";
echo "<input type='hidden' name='email' value='{$_GET['email']}'>";

if(isset($_GET['name'])) 内部