简体   繁体   English

session 中的复选框表单无法正常工作

[英]checkbox form in a session doesn't work properly

I'm trying to write the code for an online form:我正在尝试为在线表单编写代码:

On page 1 there are just some demographic questions.在第 1 页上,只有一些人口统计问题。 It's on that first page I start a session.在第一页上,我启动了 session。 When they submit their answers they get redirected to page 2 where I place some statements on which they can select their level of agreement.当他们提交答案时,他们会被重定向到第 2 页,我在其中放置了一些声明,他们可以在这些声明上 select 他们的协议级别。 - In case they want to go back their answers on the previous page are remembered thanks to the session. - 如果他们想要 go 回复他们在上一页上的答案,由于 session,他们会记住。 - Ultimatly their answers will be transferred to a database (haven't written that yet). - 最终他们的答案将被转移到数据库中(还没有写)。

Problem: 1) I tried to multiply the amount of questions to 3 questions on page 2 but when I go to page 3 and return to page 2 the answers aren't remembered?问题:1)我试图将第 2 页上的问题数量乘以 3 个问题,但是当我将 go 转到第 3 页并返回第 2 页时,答案不记得了吗?

2) If I only check some of the questions (not all of them) I get 'Notice: Undefined index:' with the line the unanswered question is on. 2)如果我只检查一些问题(不是全部),我会得到“通知:未定义的索引:”与未回答的问题所在的行。 Can this be solved somehow?这可以以某种方式解决吗?

This is the code from page 2:这是第 2 页的代码:

<form action="page3.php" method="post">
<?php
    $options = array(
        'Good' => 'Good',
        'Neutral' => 'Neutral',
        'Bad' => 'Bad',
);

checkbox( 'Question_1', 'Question_1', 'How good is your health?', $options );
checkbox( 'Question_2', 'Question_2', 'How good is your math?', $options );
checkbox( 'Question_3', 'Question_3', 'How good is your knowledge of astrofysics?', $options );

?>

<?php submit('Go To Step 3 &raquo;'); ?>
</form>

This is the code from page 3 (where the info from page 2 is stored in the SESSION variables):这是第 3 页的代码(第 2 页的信息存储在 SESSION 变量中):

<?php
include_once('header.php');

// Store data from page 1 in session
if ( ! empty( $_POST ) ) {
  $_SESSION['Question_1'] = $_POST['Question_1'];
  $_SESSION['Question_2'] = $_POST['Question_2'];
  $_SESSION['Question_3'] = $_POST['Question_3'];
}

?>

And this is the code of the used function (from functions.php):这是使用的 function 的代码(来自functions.php):

function checkbox( $name, $id, $label, $options = array() ) {?>
  <div class="form-group">
    <p><?php echo $label; ?></p>
    <?php foreach ( $options as $value => $title ) : ?>
      <label class="checkbox-inline" for="<?php echo $id; ?>">
        <input type="checkbox" required name="<?php echo $name; ?>[]" value="<?php echo $value; ?>" <?php isset($_SESSION['Question_1'],$_SESSION['Question_2'],$_SESSION['Question_3']) ? checked="checked":; ?>>
        <span class="checkbox-title"><?php echo $title; ?></span>
      </label>
    <?php endforeach; ?>
  </div>

What am I doing wrong?我究竟做错了什么?

The issue is with this line:问题在于这一行:

    <input type="checkbox" required name="<?php echo $name; ?>[]" value="<?php echo $value; ?>" <?php isset($_SESSION['Question_1'],$_SESSION['Question_2'],$_SESSION['Question_3']) ? checked="checked":; ?>>

It should probably be something like:它可能应该是这样的:

    <input type="checkbox" required name="<?php echo $name; ?>[]" value="<?php echo $value; ?>" <?php isset($_SESSION[$id]) ? checked="checked":; ?>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM