-1

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

第 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

4 回答 4

0

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

<?php } else if ($_POST['step'] == 3) { //do stuff  ?>
     <?php 
             echo '<pre>';
             print_r($_SESSION['hardware']);
             print_r($_SESSION['accessories']);
             echo '</pre>';
     ?>
<?php } ?>
于 2012-11-26T04:49:34.523 回答
0

表单持久化,最简单的实现,需要会话管理。即您需要将http post 值设置为会话。您实现此代码的方式将根据架构(即您使用或不使用什么框架)而有所不同,但基本上这就是您需要做的所有事情。

例如

foreach($_POST as $key=>$value){$_SESSION[$key]=$value;}
于 2012-11-26T04:31:42.227 回答
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一路丢失值, 例如:

 var_dump($_SESSION['hardware']);
 if( is_array($_SESSION['hardware']) ){
  foreach ($_SESSION['hardware'] as $val) {
     $hardwareresults .= $val.",\n";
  }
 }
 var_dump($hardwareresults);
于 2012-11-20T06:23:10.717 回答
0

从这里,很难说出你的问题是什么,但你最终做了一些奇怪和不必要的事情。

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

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

$accessoriesresults[] = $val;

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

if( is_array($_SESSION['accessories']) ){
    foreach ($_SESSION['accessories'] as $val) {
         echo "<li>$val</li>";
    }
}
于 2012-11-14T21:57:19.323 回答