获取单选按钮codeigniter的值

时间:2016-01-25 07:34:38

标签: html codeigniter

我试图通过单选按钮获取值。我有两个单选按钮1和2,每个按钮具有不同的值。当我在控制器上打印__时,即使我选择了第一个单选按钮,该值也始终是第二个单选按钮 查看

<input class="payment_method_icon" type="radio" value= "1" name="mode" checked="checked">
<input class="payment_method_icon" type="radio" value="2" name="mode">

控制器

print_r($this->input->post());

如何获得单选按钮的正确值

2 个答案:

答案 0 :(得分:0)

将您的模式转换为数组类型中的'mode []'

 <input class="payment_method_icon" type="radio" value= "1" name="mode[]">
 <input class="payment_method_icon" type="radio" value="2" name="mode[]">

  **in php code** 

    echo $this->input->post('mode');

答案 1 :(得分:0)

我不确定你的意思但是先付出代价。当您需要能够发布广播时,只需使用$this->input->post('mode'),然后您可以使用它来更新表格或插入。

TRY

<?php

class Welcome extends CI_Controller {

    public function index() {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {

            $this->update_radio();
        }

        $sample = $this->get_radio();

        $data['radio'] = $sample['radio'];

        $this->load->view('welcome_message', $data);
    }

    public function get_radio() {
        $this->db->where('radio_id', '1');
        $query = $this->db->get('radio');

        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return false;
        }
    }

    public function update_radio() {
        $data = array(
            'radio' => $this->input->post('mode')
        );

        $this->db->where('radio_id', '1');
        $this->db->update('radio', $data);
    }

}

查看

<?php echo form_open('welcome');?>

<?php if ($radio == 0) {?>
<input class="payment_method_icon" type="radio" value= "0" name="mode" checked="checked"> No
<?php } else { ?>
<input class="payment_method_icon" type="radio" value="0" name="mode"> No   
<?php }?>

<?php if ($radio == 1) {?>
<input class="payment_method_icon" type="radio" value= "1" name="mode" checked="checked"> Yes
<?php } else { ?>
<input class="payment_method_icon" type="radio" value="1" name="mode"> Yes  
<?php }?>

<br/>
<br/>
<button type="submit" >Save</button>
<?php echo form_close();?>