按下提交后网站从头开始刷新

after submit is pressed site refreshes from the beginning

我是这里和 PHP 的新人,我试图 "simple" 测试我的技能,但此时我无法自拔:

<?php
error_reporting(0);
//zweiter Formular test

//$anzahl = $_POST['anzahl'];

function step1($anzahl){
    if($anzahl > 0){
        echo '<h3>Schritt 2</h3>';
        echo '<p>Alles klar, bitte gib den<b> ' . $anzahl . '</b> Feldern namen </p>';
        echo '<form action="formular1.php" method="post">';
            for($i=1; $i<=$anzahl; $i++){

                echo $i . '. Feld <input name="' . $i . '_feld"/> <br/>';

                }
        echo '<p> <input name="anzahl" type="hidden" value=' . $anzahl . ' />
        <input name="submit1" type="submit" /><input type="reset" /></p></form>';

        if(isset($_POST['submit1']))
            step2($_POST['anzahl']);
    }else
        echo '';
}

function step2($anzahl){
    echo '<h3>Schritt 3</h3>';
    echo '<p>Alles klar, hier dein Formular:</p>';

    $feld_namen = array();

    //Trägt post-werte in array ein
    for($j=1; $j <= $anzahl; $j++){
        $feld_namen[$j] = $_POST[$j . '_feld'];
        }

    //print_r($feld_namen);

    echo '<form action="formular1.php" method="post">';
        for($i=1; $i<=$anzahl; $i++){
                //array hier lesen      
                //echo $i . '. <input name="' . $i . '_feld_value" /> <b>' . $_POST[$i . '_feld'] . '</b>  <br/>';
                echo $i . '. <input name="' . $i . '_faled_value" /> <b>' . $feld_namen[$i] . ' </b> <br/>';
            }
        echo '<p><input name="submit2" type="submit" /><input type="reset" /></p></form>';

        if(isset($_POST['submit2']))
            step3();
}

function step3(){
    echo '<h3>Schritt 3</h3>';
    }

?>

问题从函数第 2 步开始,也许我只是瞎了眼,但它说当设置名称为 submit2 的按钮时,页面应该刷新并实际转到第 3 步。但它并没有,页面只是从头开始。

我是在想太多 Java 还是什么?

我知道它不是一个很好的脚本,但我正在逐步尝试

脚本应该从这里开始:

<h3>Schritt 1</h3>
<form action="formular1.php" method="post">
<p>Wieviele eingabe Felder brauchst du ?</p>
<p>Ich brauche <input name="anzahl" size="2px" /> Felder</p>
<p><input type="submit" /><input type="reset" /></p>
</form>
<?php step1($_POST['anzahl']) ?>    

假设你展示的是第二步。现在用户单击提交按钮,$_POST['submit2'] 将被设置。但是,$_POST['submit1'] 将不再设置(因为用户没有点击它)。

因此您的代码将永远不会激活 step2(),而 step3() 需要被调用。您可以使用隐藏的输入字段轻松解决此问题。

<input type="hidden" name="submit1">

在你的第 2 步中。但更简洁的解决方案是以另一种方式调用这些方法。比如这样

if (isSet($_POST['submit1'])) {
    step2();
} elseif (isSet($_POST['submit2'])) {
    step3();
} else {
    step1();
}