通过分步表单传递复选框结果

时间:2012-11-14 16:28:18

标签: php

我有一个一步一步的表格,我希望将复选框结果传递到最后的评论页面。

步骤1和2中有复选框,如何在步骤3中显示这些复选框?

我试图在下面这样做,但无法让它回应结果。

<form class="form" method="POST" action="<?php the_permalink(); ?>">
<? if (!$_POST['step']) { ?>
<input type="hidden" name="step" value="1" />
<div class="steps" style="float:left;">
  <p style="font-size:17px!IMPORTANT;"><b>Step 1 of 3</b></p>
</div>
<div class="progress-buttons" style="float:right;">
  <button class="next" type="submit" name="submit">Next</button>
</div>
<div class="clear"></div>
<?php $posts = get_field('options');
                            if( $posts ):
                            $items = 0;
                            foreach( $posts as $post): // variable must be called $post (IMPORTANT)
                                setup_postdata($post); ?>
<p style="clear:right;float:right;margin-right:60px;">
  <input type="checkbox" name="hardware[]" value="<?php the_title(); ?>">
  Select</p>
<?php $items++; endforeach;
                            wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                            endif; ?>
</div>
</div>
<? } else if ($_POST['step'] == 1) {
foreach($_POST as $name => $value) {
    if ($name == "hardware") {
        $_SESSION[$name] = $_POST[$name];
    } else if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
} ?>
<input type="hidden" name="step" value="2" />
<div class="steps" style="float:left;">
  <p style="font-size:17px!IMPORTANT;"><b>Step 2 of 3</b></p>
</div>
<div class="progress-buttons" style="float:right;"> <a class="back" href="<?php the_permalink(); ?>?<?= $field ?>" >Back</a>
  <button class="next" type="submit" name="submit">Next</button>
</div>
<?php $posts = get_field('accessories');
                            if( $posts ):
                            $items = 0;
                            foreach( $posts as $post): // variable must be called $post (IMPORTANT)
                                setup_postdata($post); ?>
<p style="clear:right;float:right;margin-right:60px;">
  <input type="checkbox" name="accessories[]" value="<?php the_title(); ?>">
  Select</p>
<?php $items++; endforeach;
                            wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                            endif; ?>
</div>
</div>
<? } else if ($_POST['step'] == 2) {
foreach($_POST as $name => $value) {
    if ($name == "accessories") {
        $_SESSION[$name] = $_POST[$name];
    } else if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
} ?>
<input type="hidden" name="step" value="3" />
<div class="steps" style="float:left;">
  <p style="font-size:17px!IMPORTANT;"><b>Step 3 of 3</b></p>
</div>
<div class="progress-buttons" style="float:right;"> <a class="back" href="<?php the_permalink(); ?>?<?= $field ?>" >Back</a>
  <button class="next" type="submit" name="submit">Next</button>
</div>
<div class="clear"></div>
<p>System spec</p>
<?php
$hardware = $_POST['hardware'];
$accessories = $_POST['accessories'];
if( is_array($_SESSION['hardware']) ){
    foreach ($_SESSION['hardware'] as $val) {
        $hardwareresults .= $val.",\n";
    }
}
if( is_array($_SESSION['accessories']) ){
    foreach ($_SESSION['accessories'] as $val) {
        $accessoriesresults .= $val.",\n";
    }
}
?>
<ul>
  <li><?php echo $hardwareresults; ?></li>
  <li><?php echo $accessoriesresults; ?></li>
</ul>
<? } else if ($_POST['step'] == 3) { //do stuff ?>
Last step
<?php } ?>

4 个答案:

答案 0 :(得分:0)

从这一点来说,说出你的问题有点困难,但你最终会做一些奇怪的和不必要的事情。

$accessoriesresults .= $val.",\n";

。=是一个字符串运算符,不要用它来连接数组。所以相反:

$accessoriesresults[] = $val;

但你为什么要这样做呢?您已经在$ _SESSION中拥有这些值,为什么要创建另一个类似的数组呢?为什么不在$ _SESSION打印出什么?

if( is_array($_SESSION['accessories']) ){
    foreach ($_SESSION['accessories'] as $val) {
         echo "<li>$val</li>";
    }
}

答案 1 :(得分:0)

您似乎正在覆盖$ _SESSION键:

foreach($_POST as $name => $value) {

 if ($name == "hardware") {
   //here you just overwrite the 'hardware' key continuously
   $_SESSION[$name] = $_POST[$name];
 } else if ($name <> "step") {
   echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
 }

} 

写出来看起来像:

 if ($name == "hardware") {
   //here you just overwrite the 'hardware' key continuously
   $_SESSION['hardware'] = $_POST['hardware'];
 }

您是存储多个值还是只存储一个值?

此外,您应该使用var_dump检查您是否在丢失值 e.g:

 var_dump($_SESSION['hardware']);
 if( is_array($_SESSION['hardware']) ){
  foreach ($_SESSION['hardware'] as $val) {
     $hardwareresults .= $val.",\n";
  }
 }
 var_dump($hardwareresults);

答案 2 :(得分:0)

表单持久性,最简单的实现,需要会话管理。即你需要将http post值设置为session。实现此代码的方式因架构而异(即您使用的框架与否),但基本上就是您需要做的全部。

e.g。

foreach($_POST as $key=>$value){$_SESSION[$key]=$value;}

答案 3 :(得分:0)

您的脚本中似乎没有任何问题,您正在将值正确地推送到会话中,只需在第三步中使用这些会话值,请尝试以下代码

<?php } else if ($_POST['step'] == 3) { //do stuff  ?>
     <?php 
             echo '<pre>';
             print_r($_SESSION['hardware']);
             print_r($_SESSION['accessories']);
             echo '</pre>';
     ?>
<?php } ?>