2012-09-20 70 views
0

後退按鈕我創建了一個2步的形式用下面的代碼:創建形式

<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/"> 
    <? if (!$_POST['step']) { ?> 
    <h1>Step 1 of 2</h1><br /> 
    <input type="hidden" name="step" value="1" />    
    <table border="0" width="100%"> 
     <tr> 
      <td> 
       <input type="text" name="title" id="title" value="Title*" /> 
      </td> 
     </tr> 
    </table>   
    <button class="continue-button" type="submit" name="submit">Continue</button> 
    <? } else if ($_POST['step'] == 1) { 
     foreach($_POST as $name => $value) { 
     if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; } 
    } ?> 
    <h1>Step 2 of 2</h1><br /> 
    <input type="hidden" name="step" value="2" /> 
    <table border="0" width="100%"> 
     <tr> 
      <td> 
       <input type="text" name="name" id="name" value="Name*" /> 
      </td> 
     </tr> 
    </table>   
    <button class="continue-button" type="submit" name="submit">submit</button> 
    <? } else if ($_POST['step'] == 2) { //do stuff 
     echo "Do stuff here"; 
    } ?> 
</form> 

如何添加在第2步後退按鈕?將會有幾個步驟不僅添加2個,所以用戶需要能夠前後移動,同時保持已填充的內容。

+0

我刪除了我的答案。請更新您的問題,以顯示您想要兩個以上的步驟,並能夠瀏覽它們。我將開始一個新的答案。 –

回答

2

試試這個

<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/"> 
    <? if (!$_POST['step']) { ?> 
    <h1>Step 1 of 2</h1><br /> 
    <input type="hidden" name="step" value="1" />    
    <table border="0" width="100%"> 
     <tr> 
      <td> 
       <input type="text" name="title" id="title" value="<?= $_REQUEST["title"]?>" placeholder="Title*" /> 
      </td> 
     </tr> 
    </table>   
    <button class="continue-button" type="submit" name="submit">Continue</button> 
    <? } else if ($_POST['step'] == 1) { 
     $field =""; 
     foreach($_POST as $name => $value) { 
     if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; $field .= $name."=".$value."&";} 
    } ?> 
    <div><a href="<?php bloginfo('url'); ?>/contact-us/?<?= $field ?>" >Back</a></div> 
    <h1>Step 2 of 2</h1><br /> 
    <input type="hidden" name="step" value="2" /> 
    <table border="0" width="100%"> 
     <tr> 
      <td> 
       <input type="text" name="name" id="name" value="Name*" /> 
      </td> 
     </tr> 
    </table>   
    <button class="continue-button" type="submit" name="submit">submit</button> 
    <? } else if ($_POST['step'] == 2) { //do stuff 
     echo "Do stuff here"; 
    } ?> 
</form> 
+0

太棒了,謝謝。 (也不知道你可以做'placeholder =「text」')。 – Rob

+0

非常歡迎:) – Champ

0

要前後導航,您需要將數據從先前的表單又回到了它,因此您需要將每個表單的值存儲在某處。你最好的選擇是創建一個session並將每個表單信息存儲在一個會話變量中。然後,當您加載每個表單時,您應該檢查數據是否已存儲在會話中。

然後你可以添加一個jQuery按鈕來改變步驟的值並提交表單。 要做到這一點,你需要在「步」隱藏輸入像「步」

<input type="hidden" name="step" id="step" value="2" /> 

,然後一個id添加一個按鈕,像

<button class="back-button" type="submit" name="back" onclick="$('#step').val('1');">Back</button> 

這將改變階躍輸入的值和然後將表格提交回到以前的表格。

您需要另一個隱藏的輸入來告訴表單來自哪裏,以便知道是否查看會話數據(您要倒退)還是POST數據(如果您前進)。

+0

對不起,應該在問題中更加清楚......將會有兩個以上的步驟,所以他們需要能夠通過表單向前和向後移動。 – Rob

1

首先將其添加在你的頁面的頂部

// keep the data 
$_SESSION['data'] = $_POST; 
$_SESSION['step'] = $_REQUEST['step']; 

同時更換if聲明是這樣的:

if ($_REQUEST['step'] == 1) { 
    // continue 

寫動態值在你的表格上

<input type="text" name="title" id="title" 
value="<?php echo $_SESSION['data']['title']; ?>" /> 

使用BackNext一個link

<a href="/?step=<?php echo $_SESSION['step'] - 1; ?>">Back</a> 

<a href="/?step=<?php echo $_SESSION['step'] + 1; ?>">Next</a> 

我想這會爲你的作品! :)