在SESSION中保存表单提交

时间:2013-11-21 10:32:52

标签: php forms session if-statement session-variables

我正在处理一个客户可以填写其尺寸的过程,然后可以提交一个表格,然后将该数据保存在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']))(一个维度字段)并刷新页面,则配置几乎就在那里。但我想使用保存的提交会话变量来检查表单是否已提交。有人能告诉我我做错了吗?

<?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>   

2 个答案:

答案 0 :(得分:1)

您是否在不进行POST的情况下刷新同一页面?

可能是由于此处的代码:

$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; ?>

答案 1 :(得分: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; ?>