【问题标题】:Codeigniter : passing radio value to databaseCodeigniter:将无线电值传递给数据库
【发布时间】:2015-04-23 03:40:36
【问题描述】:

我想做一个简单的考试系统。

我们的想法是从下面的表格中提出一个问题,然后将值与下面的数据一起发送,因此我想将检查的输入发送到名为正确答案的列,并将两者发送到名为选项的列。

有什么想法吗?

我在视图中有这个表单

<form action="<?php echo base_url('home') ;?>" method="post">
    <input type="text" name="question" placeholder="Enter question here"><br>
    <label>choose the right answer</label><br>
    <input type="radio" name="answer" value="first"> <input type="text" placeholder="first choice"><br>
    <input type="radio" name="answer" value="second" checked><input type="text" placeholder="second choice"><br>
    <input type="submit" value="sumbit" name="ask">
</form>

这是我的控制器:

class Home extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->model('exams_model') ;
    }
    public function index(){
        $this->load->view('vieww') ;
        if($this->input->post('ask')) {
            $this->exams_model->add_question() ;
        }
    }

}

&这是模型:

class Exams_model extends CI_Model{
    public function add_question(){
        $data = array(
            'question' => $this->input->post('question') ,
            'choices' => $this->input->post('#') , // here i want to send both inputs ( the checked and non checked input
            'right_answer' => $this->input->post('#') // i want to pass the checked input to  the column right answer in the database 


        );
        $insert = $this->db->insert('exams' , $data);
        return $insert ;

    }
}

【问题讨论】:

    标签: php codeigniter radio


    【解决方案1】:

    实际上,您不能发送单选按钮的未选中值。
    但是您可以做的是:
    创建隐藏字段,将它们命名为choice_1 和choice_2,
    将它们设置为与您为单选按钮选项设置的值相同的值。

    <input type="hidden" name='choice_1' value='first_value'/> 
    <input type="hidden" name='choice_2' value='second_value'/>
    
    <input type="radio" name='answer' value='first_value'/> 
    <input type="radio" name='answer' value='second_value'/> 
    

    现在像以前一样在模型中获取它们的对应值。

     $data = array(
            'question' => $this->input->post('question') ,
            'choices' => $this->input->post('choice_1').' and '.$this->input->post('choice_2') , 
            'right_answer' => $this->input->post('#') 
        );
    

    如果觉得有帮助,请投票 :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2017-01-18
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多