【问题标题】:Php Newbie: Add inpux box to arrayPHP新手:将输入框添加到数组
【发布时间】:2018-12-07 12:23:59
【问题描述】:

如何将姓名和电子邮件的输入框添加到数组中以显示/打印/回显姓名和电子邮件以及答案。我能够做的是显示答案,但不显示数组中的输入框内容。我是 php 的新手,所以请耐心等待并帮助我采取我需要建立能力的婴儿步骤。非常感谢您抽出宝贵时间提供帮助。

<?php 




   $Questions = array(
    1 => array(
        'Question' => '1. Do you have the signage displayed in the facility? ',
        'Answers' => array(
            'A' => 'A. There is a signage displayed',
            'B' => 'B. There is a signage but this is not displayed',
            'C' => 'C. There is no signage',
            'D' => 'D. Not Applicable'
        ),
        'CorrectAnswer' => 'A'
    ), 

     2 => array(
        'Question' => '2. Does your signage provide all needed information?',
        'Answers' => array(
            'A' => 'A. Yes it does',
            'B' => 'B. Some part of it',
            'C' => 'C. Not at all.',
            'D' => 'D. Not Applicable'

        ),
        'CorrectAnswer' => 'A'
    )
); 

    if (isset($_POST['answers'])){
        $Answers = $_POST['answers']; // Get submitted answers.

        // Now this is fun, automated question checking! ;)

        foreach ($Questions as $QuestionNo => $Value){
            // Echo the question
            echo $Value['Question'].'<br />';




            if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){

                 echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
                 echo 'The Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
            } else {
                echo 'The Correct answer is : <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
                echo 'You got it correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; 

                //$counter++;

            }

            echo '<br /><hr>'; 
                                    if ($counter='') 
                                    { 
                                    $counter='0';
                                    $results = "Your score: $counter/2"; 
                                    }
                                    else 
                                    { 
                                    $results = "Your score: $counter/2"; 
                                    }
                }                           echo $results;
    } else {  
    ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">

            Name: <input type="text" name="name"><br>
            Email: <input type="text" name="email"><br>






        <?php foreach ($Questions as $QuestionNo => $Value){ ?>

            <h3><?php echo $Value['Question']; ?></h3>
            <?php 
              echo '$name' .'<br />';

                foreach ($Value['Answers'] as $Letter => $Answer){ 
                $Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
            ?>
            <div>
                <input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
                <label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
            </div>
            <?php } ?>

        <?php } ?>
        <br /> 
        <input type="submit" value="Submit And View Suggestions Now" />
        </form>
    <?php 
    }
    ?>

【问题讨论】:

  • 那么源代码输出是什么?您至少可以发布输入/标签部分吗?!
  • 您可以从$_POST['name']$_POST['email']获取姓名和电子邮件

标签: php arrays forms


【解决方案1】:

我猜你的意思是你在评估中找不到答案(A、B、C 或 D)。要创建无线电组,您必须为所有无线电输入提供相同的名称。看看:https://www.w3.org/WAI/tutorials/forms/grouping/

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多