多步表单如何使用不同页面之间的信息

时间:2013-07-17 08:05:14

标签: php forms

我尝试在php中使用多集形式,因为我不知道jquery和javascript(并且难以理解它)。

首先,我想做什么:

第1页:

Q1你想要多少个号码?   例如,用户输入“2”

第2页:

Q2您想要的每个号码的字母数:

(看起来像:数字1:---数字2:----)

例如,

用户输入第一个“2”,第二个输入“1”。

第3页:

(看起来像:)

1号:字母1:---字母2:----

2:字母1:---

到目前为止,第1页和第2页之间的一切运作良好,但第2页和第3页之间没有任何关系 另一个问题是,如果用户在3和2之间“返回”,则页面2没有任何内容,因为第一页的值“丢失”

我正在寻找很长时间但没有找到任何东西,如果你有任何建议我会很高兴。

这是我的代码:

   <?php
   if ( isset( $_POST["step"] ) and $_POST["step"] >= 1 and $_POST["step"]<= 4 ) {
call_user_func( "processStep" . (int)$_POST["step"] );
   } else {
displayStep1();
    }
    function processStep1() {
displayStep2();
    }
   function processStep2() {
if ( isset( $_POST["submitButton"] ) and $_POST["submitButton"] =="< Back" ) {
    displayStep1();
} else {
    displayStep3();
}
    }
   function processStep3() {
if ( isset( $_POST["submitButton"] ) and $_POST["submitButton"] =="< Back" ) {
    displayStep2();
} else {
    displayThanks();
}
     }
    function displayStep1() {
    ?>
<h1>Step 1</h1>

<form action="" method="post">

<input type="hidden" name="step" value="1" />
<input type="hidden" name="number2" value="" />

<label for="number">Combien de chiffres?</label>
<input type="text" name="number" id="number" value=" " />

<div style="clear: both;">
<input type="submit" name="submitButton" id="nextButton" value="Next" />

</form>

<?php
}
function displayStep2() {
?>

<h1>Step 2</h1>
<form action="" method="post">

<input type="hidden" name="step" value="2" />
<input type="hidden" name="number" value="" />
<label for="number2">Combien de lettre pour chaque chiffres?</label>

<?php 
$num = $_POST['number'];
    for ($i = 1; $i <= $num; $i++) {
        echo "Numero ";
        echo $i;
?>  

<input type="text" name="number2" id="number2"/>

<?php
}
?>

<div style="clear: both;">
<input type="submit" name="submitButton" id="nextButton" value="Next &gt;" />
<input type="submit" name="submitButton" id="backButton" value="&lt; Back" />
</form>

<?php
}
function displayStep3() {
?>

<h1>Step 3</h1>
<form action="" method="post">

<input type="hidden" name="step" value="3" />
<input type="hidden" name="number" value="" />
<input type="hidden" name="number2" value="" />
<label for="number3">Choisir lettres</label>

<?php 
$num = $_POST['number'];
    for ($i = 1; $i <= $num; $i++) {
        echo "Numero ";
        echo $i;

    $num2 = $_POST['number2'];
        for ($j = 1; $j <= $num2; $j++) {
            echo "Lettre ";
            echo $j;
?>  

<input type="text" name="number3" id="number3"/>

<?php
}
}
?>

<div style="clear: both;">
<input type="submit" name="submitButton" id="nextButton" value="Next &gt;" />
<input type="submit" name="submitButton" id="backButton" value="&lt; Back" />
</form>

<?php
}
function displayThanks() {
?>

<h1>Merci</h1>

<?php
}
?>

0 个答案:

没有答案