如何使用 codigniter 在同一变量发布方法名称中插入单选、复选框和文本框值?



我的视图页面

<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST"> 
<label>Who is usa president</label>
<input type="text" name="answer[]">
<label>Dhoni is cricket player player</label>
<input type="radio" name="answer[]" value="True">True
<input type="radio" name="answer[]" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="answer[]" value="newyork">newyork
<input type="checkbox" name="answer[]" value="india">india
<input type="checkbox" name="answer[]" value="srilanka">srilanka
</form>

我的控制器页面

public function add_stu_ans()
{
$values['stu_answer']  = $this->input->post('ans');
$this->Common_model->insert_record('student',$values);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}

这是模型

public function insert_record($table,$values) {     
$this->db->insert($table,$values);
return $this->db->insert_id();  
}

请任何人都可以告诉我如何在控制器中自定义代码

仅当您采用另一个隐藏的文本字段时,您的"qstn_id"值才有可能。 将此字段放在数组中。

在编码部分,当采用foreach循环时,您必须采用"[key]"格式qstn_id否则您将找不到跳过的记录。

例如:

foreach ($que as $key => $ansValue) {
$val2 = $ansValue[$key];
}
// $val2 store as your output.

这是使用 json_encode 保存数据的代码。让我知道您收到任何错误,我没有运行此代码。

查看文件

<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST"> 
<label>Who is usa president</label>
<input type="text" name="usa_president">
<label>Dhoni is cricket player player</label>
<input type="radio" name="cricket_player" value="True">True
<input type="radio" name="cricket_player" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="country[]" value="newyork">newyork
<input type="checkbox" name="country[]" value="india">india
<input type="checkbox" name="country[]" value="srilanka">srilanka
</form>

控制器

public function add_stu_ans()
{
$usa_president = $this->input->post('usa_president');
$cricket_player= $this->input->post('cricket_player');
$country= $this->input->post('country');
$final_data = array(
'usa_president' => $usa_president,
'cricket_player' => $cricket_player,
'country' => $country,
);
$json_encode_data = json_encode($final_data);
$this->Common_model->insert_record('student',$json_encode_data);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}

请在结束和从数据库获取数据时检查这一点,然后您需要从函数json_decode传递返回数据。

$json_decode_data = json_decode($return_data_from_data_table);

最新更新