【问题标题】:Validate correct answer and add marks in PHP quiz验证正确答案并在 PHP 测验中添加标记
【发布时间】:2015-11-03 09:45:53
【问题描述】:

我正在开发一个简单的测验网络应用程序,我想验证提交按钮上的每一个问题,显示正确并为正确的问题添加标记。

我已经创建了这样的第一个问题页面

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Cric Quiz </title>

    <link href="<?php echo base_url(); ?>assets/style.CSS" rel="stylesheet" type="text/css"/>

    </head>
<body>

<div>
    <h1>Cric Quiz</h1>
</div>

<div id='quesNo'>
    <center>Question 01</center>
</div>

<div id='questions'>
    <center>When did Australia win their first ever world cup?</center>
</div>

<div id='answers'>
<input type="radio" name="year" value="1992">1992<br>
<input type="radio" name="year" value="1996">1996<br>
<input type="radio" name="year" value="2000">2000<br>
<input type="radio" name="year" value="2012">2012
</div>

<center><a href="<?=base_url().'index.php/Questions/question_2';?>" class="button1">Next</a></center>

</body>
</html>

我的控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Questions extends CI_Controller {

public function question_1()
    {

        $this->load->view('quiz_one');

    }

    public function question_2()
    {

        $this->load->view('quiz_two');

    }
}

如何在我的控制器中验证我的答案

PS:我正在使用 codeigniter

【问题讨论】:

    标签: php html codeigniter validation


    【解决方案1】:

    你的方法是错误的。只需要一个函数并将问题 id 传递给它。如果明天你有 1000 个问题,你会创建尽可能多的函数吗?

    先将正确答案存入数据库。

    接下来你的 html 应该有表单,提交带有帖子数据的表单

    在表单中发布问题 ID

    在控制器中添加此代码,

    // run query to fetch data from database based on the question in
    if($this->input->post("year") == "value from database"){
    
    } else {
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 2020-01-16
      • 2015-09-06
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多