2013-11-21 79 views
0

我正在開發一個客戶可以填寫他的維度,然後可以提交一個表格,然後將數據保存在SESSION變量中並顯示在頁面上的過程,因此客戶可以觀看他的配置。保存表格在SESSION中提交

我保存我在$ _SESSION ['wz_submit_saving_1']中提交併使用If語句來檢查表單是否已提交,以便可以顯示配置。

<?php // Save submit 
$wz_submit_saving_1 = $_POST['wz_submit_saving_1']; 
$_SESSION['wz_submit_saving_1'] = $wz_submit_saving_1; 

if(isset($_SESSION['wz_submit_saving_1'])) : 

    // Save wz_saving_a in session 
    $wz_saving_a = $_POST['wz_saving_a']; 
    $_SESSION['wz_saving_a'] = $wz_saving_a; 

    // Save wz_saving_b in session 
    $wz_saving_b = $_POST['wz_saving_b']; 
    $_SESSION['wz_saving_b'] = $wz_saving_b; 

endif; ?> 

我的問題是,如果我使用if(isset($ _ SESSION [「wz_submit_saving_1」]))用於顯示配置,它顯示以及提交後,但如果我刷新頁面的配置已經一去不復返了。

如果我使用if(isset($ _ SESSION ['wz_saving_a']))(a維字段)並刷新頁面,配置幾乎就在那裏。但我想使用保存的提交會話變量來檢查表單是否已提交。有人能告訴我我做錯了什麼嗎?

<?php if(isset($_SESSION['wz_submit_saving_1'])) : ?> 

<div id="wz_config_1" class="wz_config"> 

    <ul> 
     <li>Dimensions</li> 
     <li>A: <?php if(isset($_SESSION['wz_saving_a'])) : echo $_SESSION['wz_saving_a']; endif; ?></li> 
     <li>B: <?php if(isset($_SESSION['wz_saving_b'])) : echo $_SESSION['wz_saving_b']; endif; ?></li> 
    </ul> 

</div><!--End wz_config_1--> 

<?php endif; ?> 

形式:

<form method="POST"> 

    <label>A</label> 
    <input name="wz_saving_a" type="text" /> 

    <label>B</label> 
    <input name="wz_saving_b" type="text" /> 

    <input name="wz_submit_saving_1" type="submit" class="add_button" value="Add" /> 

</form> 
+1

哪裏是你的'在session_start()'? – naththedeveloper

+0

對不起,在我的頁面頂部。 – Robbert

回答

1

你刷新同一頁面沒有做一個帖子?

這可能是由於這裏的代碼:

$wz_submit_saving_1 = $_POST['wz_submit_saving_1']; 
$_SESSION['wz_submit_saving_1'] = $wz_submit_saving_1; 

因爲你只是在做一個刷新GET,會話變量wz_submit_saving_1是由空wz_submit_saving_1 POST變量覆蓋。

編輯:

<?php // Save submit 
if(isset($_POST['wz_submit_saving_1'])) : 
    $_SESSION['wz_submit_saving_1'] = $_POST['wz_submit_saving_1']; 

    // Save wz_saving_a in session 
    $wz_saving_a = $_POST['wz_saving_a']; 
    $_SESSION['wz_saving_a'] = $wz_saving_a; 

    // Save wz_saving_b in session 
    $wz_saving_b = $_POST['wz_saving_b']; 
    $_SESSION['wz_saving_b'] = $wz_saving_b; 

endif; ?> 
+0

是的,我刷新沒有做POST。所以你說我不能用提交會話變量來檢查它? – Robbert

+0

如果您正在刷新到同一頁面,那麼'wz_submit_saving_1'變量將始終以空值結束(因爲您沒有在刷新時提供任何POST變量,只有GET)。 – russellc

+0

好的,我明白了。那麼你的建議是什麼?使用其他變量進行檢查? – Robbert

0
<?php // Save submit 
if(isset($_POST['wz_submit_saving_1'])){ 
    $_SESSION['wz_submit_saving_1'] = $_POST['wz_submit_saving_1']; 
} 
if(isset($_SESSION['wz_submit_saving_1'])) : 

    // Save wz_saving_a in session 
    $wz_saving_a = $_POST['wz_saving_a']; 
    $_SESSION['wz_saving_a'] = $wz_saving_a; 

    // Save wz_saving_b in session 
    $wz_saving_b = $_POST['wz_saving_b']; 
    $_SESSION['wz_saving_b'] = $wz_saving_b; 

endif; ?> 
+0

這不起作用,得到了同樣的問題.. – Robbert