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

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

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

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

> Controller

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

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

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:

确定