douhong4452 2018-12-04 09:12
浏览 29

带输入框的Php问卷

I am trying to write a script that captures the name & email address of participants into a plain text file and also displays the name/textbox contents before/above the answers after pressing the submission button. am new to PHP so am a little lost. below is my code :

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法