如何从代码点火器形式获取单选按钮值到控制器?

时间:2014-01-27 18:51:21

标签: php codeigniter codeigniter-form-helper

在视图中

$male=array('id'=>'2','name'=>'gen');
$female=array('id'=>'3','name'=>'gen');
echo "<label>Gender : <label>"." ".form_radio($male)."Male".form_radio($female)."Female"."<br>";

我怎样才能在控制器中得到它?

我的控制器

public function index()
{
    $this->load->view('include/header');
    $this->load->helper('form');
    $this->load->view('data');
    $this->load->view('include/footer');
}

public function formHandler()
{
    $data = array('name'=>$_POST['name'],'age'=>$_POST['age'],'gen'=>$_POST['gen'],'a‌​dd'=>$_POST['add']);
    $this->load->library('form_validation');
    echo $data['name'];
    //echo $data['age'];
    //echo $data['add'];
}

3 个答案:

答案 0 :(得分:4)

要从单选按钮获取值,您需要为它们分配value。例如,

<form action="/formHandler" method="post">
$male=array('id'=>'2','name'=>'gen', 'value'=>'m');  
$female=array('id'=>'3','name'=>'gen', 'value'=>'f');  
echo "<label>Gender : <label>"." ".form_radio($male)."Male".form_radio($female)."Female"."<br>";
<input type="submit">
</form>

然后在控制器中调用

$var = $this->input->post('gen');

答案 1 :(得分:1)

使用

$radio_value = $this->input->post('gen');

答案 2 :(得分:0)

$radio_value = $this->input->post('gen');