英文:
How to insert radio, checkbox and textbox value in same variable post method name using codigniter?
问题
以下是您要翻译的内容:
我的视图页面
<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
<label>美国总统是谁</label>
<input type="text" name="answer[]">
<label>Dhoni是板球运动员</label>
<input type="radio" name="answer[]" value="True">True
<input type="radio" name="answer[]" value="False">False
<label>哪个国家位于亚洲</lable>
<input type="checkbox" name="answer[]" value="newyork">纽约
<input type="checkbox" name="answer[]" value="india">印度
<input type="checkbox" name="answer[]" value="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' , '您的数据已插入');
redirect('Quiz/question');
}
这是模型
public function insert_record($table,$values) {
$this->db->insert($table,$values);
return $this->db->insert_id();
}
请注意,上述翻译是对给定代码的直译,可能需要根据上下文进行进一步调整和自定义。如果需要更多详细信息或自定义代码,请提供更多信息。
英文:
my view page
<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>
My controller page
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');
}
And this is model
public function insert_record($table,$values) {
$this->db->insert($table,$values);
return $this->db->insert_id();
}
please anyone can tell me how to customize the code in controller
答案1
得分: 1
只有在您添加另一个隐藏文本字段,其中您的 'qstn_id' 将作为值存在时才可能。将此字段放入数组中。
在编码部分,当使用 foreach 循环时,您必须将 qstn_id 作为 "[key]" 格式来处理,否则无法找到跳过的记录。
例如:
foreach ($que as $key => $ansValue) {
$val2 = $ansValue[$key];
}
// $val2 作为您的输出存储。
英文:
Only possible if you take another hiddent text field where your 'qstn_id' will be in value. take this field in array.
in coding part while taking foreach loop you have to take qstn_id as in "[key]" format otherwise you cant find skipped records.
for eg. :
foreach ($que as $key => $ansValue) {
$val2 = $ansValue[$key];
}
// $val2 store as your output.
答案2
得分: 1
这是使用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);
英文:
This is code to save the data using json_encode. Let me know you get any error I have not run this code.
> View File
<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>
> Controller
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');
}
Please check this at your end and time of fetch the data from the database then you need to pass that return data from the function json_decode.
$json_decode_data = json_decode($return_data_from_data_table);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论