PHP新手:将Inpux框添加到数组

时间:2018-12-07 12:23:59

标签: php arrays forms

如何将名称和电子邮件输入框添加到数组以显示/打印/回显名称和电子邮件以及答案。我所能做的是显示答案,而不显示数组中的输入框内容。我是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 
    }
    ?>

1 个答案:

答案 0 :(得分:0)

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