带输入框的PHP问卷

时间:2018-12-04 09:12:23

标签: php 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 
    }
    ?>

0 个答案:

没有答案