How to insert radio, checkbox and textbox value in same variable post method name using codigniter?

huangapple go评论92阅读模式
英文:

How to insert radio, checkbox and textbox value in same variable post method name using codigniter?

问题

以下是您要翻译的内容:

我的视图页面

  1. <form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
  2. <label>美国总统是谁</label>
  3. <input type="text" name="answer[]">
  4. <label>Dhoni是板球运动员</label>
  5. <input type="radio" name="answer[]" value="True">True
  6. <input type="radio" name="answer[]" value="False">False
  7. <label>哪个国家位于亚洲</lable>
  8. <input type="checkbox" name="answer[]" value="newyork">纽约
  9. <input type="checkbox" name="answer[]" value="india">印度
  10. <input type="checkbox" name="answer[]" value="srilanka">斯里兰卡
  11. </form>

我的控制器页面

  1. public function add_stu_ans()
  2. {
  3. $values['stu_answer'] = $this->input->post('ans');
  4. $this->Common_model->insert_record('student',$values);
  5. $this->session->set_flashdata('message_name' , '您的数据已插入');
  6. redirect('Quiz/question');
  7. }

这是模型

  1. public function insert_record($table,$values) {
  2. $this->db->insert($table,$values);
  3. return $this->db->insert_id();
  4. }

请注意,上述翻译是对给定代码的直译,可能需要根据上下文进行进一步调整和自定义。如果需要更多详细信息或自定义代码,请提供更多信息。

英文:

my view page

  1. <form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
  2. <label>Who is usa president</label>
  3. <input type="text" name="answer[]">
  4. <label>Dhoni is cricket player player</label>
  5. <input type="radio" name="answer[]" value="True">True
  6. <input type="radio" name="answer[]" value="False">False
  7. <label>Which is asian country</lable>
  8. <input type="checkbox" name="answer[]" value="newyork">newyork
  9. <input type="checkbox" name="answer[]" value="india">india
  10. <input type="checkbox" name="answer[]" value="srilanka">srilanka
  11. </form>

My controller page

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

And this is model

  1. public function insert_record($table,$values) {
  2. $this->db->insert($table,$values);
  3. return $this->db->insert_id();
  4. }

please anyone can tell me how to customize the code in controller

答案1

得分: 1

只有在您添加另一个隐藏文本字段,其中您的 'qstn_id' 将作为值存在时才可能。将此字段放入数组中。

在编码部分,当使用 foreach 循环时,您必须将 qstn_id 作为 "[key]" 格式来处理,否则无法找到跳过的记录。

例如:

  1. foreach ($que as $key => $ansValue) {
  2. $val2 = $ansValue[$key];
  3. }
  4. // $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. :

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

答案2

得分: 1

这是使用json_encode保存数据的代码。如果您在运行此代码时遇到任何错误,请告诉我。

查看文件

  1. <form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
  2. <label>Who is usa president</label>
  3. <input type="text" name="usa_president">
  4. <label>Dhoni is cricket player player</label>
  5. <input type="radio" name="cricket_player" value="True">True
  6. <input type="radio" name="cricket_player" value="False">False
  7. <label>Which is asian country</lable>
  8. <input type="checkbox" name="country[]" value="newyork">newyork
  9. <input type="checkbox" name="country[]" value="india">india
  10. <input type="checkbox" name="country[]" value="srilanka">srilanka
  11. </form>

控制器

  1. public function add_stu_ans()
  2. {
  3. $usa_president = $this->input->post('usa_president');
  4. $cricket_player= $this->input->post('cricket_player');
  5. $country= $this->input->post('country');
  6. $final_data = array(
  7. 'usa_president' => $usa_president,
  8. 'cricket_player' => $cricket_player,
  9. 'country' => $country,
  10. );
  11. $json_encode_data = json_encode($final_data);
  12. $this->Common_model->insert_record('student',$json_encode_data);
  13. $this->session->set_flashdata('message_name' , 'Your Data is Inserted');
  14. redirect('Quiz/question');
  15. }

请在您的端口上检查此代码,并在从数据库检索数据时,需要通过json_decode函数传递返回的数据。

  1. $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

  1. &lt;form action=&quot;&lt;?php echo base_url()?&gt;Quiz/add_stu_ans&quot; method=&quot;POST&quot;&gt;
  2. &lt;label&gt;Who is usa president&lt;/label&gt;
  3. &lt;input type=&quot;text&quot; name=&quot;usa_president&quot;&gt;
  4. &lt;label&gt;Dhoni is cricket player player&lt;/label&gt;
  5. &lt;input type=&quot;radio&quot; name=&quot;cricket_player&quot; value=&quot;True&quot;&gt;True
  6. &lt;input type=&quot;radio&quot; name=&quot;cricket_player&quot; value=&quot;False&quot;&gt;False
  7. &lt;label&gt;Which is asian country&lt;/lable&gt;
  8. &lt;input type=&quot;checkbox&quot; name=&quot;country[]&quot; value=&quot;newyork&quot;&gt;newyork
  9. &lt;input type=&quot;checkbox&quot; name=&quot;country[]&quot; value=&quot;india&quot;&gt;india
  10. &lt;input type=&quot;checkbox&quot; name=&quot;country[]&quot; value=&quot;srilanka&quot;&gt;srilanka
  11. &lt;/form&gt;

> Controller

  1. public function add_stu_ans()
  2. {
  3. $usa_president = $this-&gt;input-&gt;post(&#39;usa_president&#39;);
  4. $cricket_player= $this-&gt;input-&gt;post(&#39;cricket_player&#39;);
  5. $country= $this-&gt;input-&gt;post(&#39;country&#39;);
  6. $final_data = array(
  7. &#39;usa_president&#39; =&gt; $usa_president,
  8. &#39;cricket_player&#39; =&gt; $cricket_player,
  9. &#39;country&#39; =&gt; $country,
  10. );
  11. $json_encode_data = json_encode($final_data);
  12. $this-&gt;Common_model-&gt;insert_record(&#39;student&#39;,$json_encode_data);
  13. $this-&gt;session-&gt;set_flashdata(&#39;message_name&#39; , &#39;Your Data is Inserted&#39;);
  14. redirect(&#39;Quiz/question&#39;);
  15. }

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.

  1. $json_decode_data = json_decode($return_data_from_data_table);

huangapple
  • 本文由 发表于 2020年1月3日 17:09:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定