2015-02-21 6 views
1

Я хочу сделать простую систему экзаменов Идея состоит в том, чтобы задать вопрос из формы ниже, а затем отправить значения с данными ниже , поэтому я хочу отправить проверенный ввод в столбце с именем правильный ответ и отправьте оба столбца с именами вариантов. любые идеи?Codeigniter: передача значения радио в базу данных

я иметь такую ​​форму в представлении

<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 ; 

    } 
} 

ответ

0

На самом деле вы не можете отправить непроверенные значения переключателя.
Но вы можете сделать следующее:
Создайте скрытые поля, назвав их как 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('#') 
    ); 

Голосуйте, если нашли полезным :-)

Смежные вопросы