我如何得到的性别单选按钮(男性,女性)的值与CodeIgniter? [英] How do I get the value of gender radio button (male, female) with CodeIgniter?

查看:148
本文介绍了我如何得到的性别单选按钮(男性,女性)的值与CodeIgniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我看来。

<input type="radio" name="male" value="0">Male</input>
<input type="radio" name="female" value="1">Female</input>

我需要做什么才能获得这些值?我需要它以后发送到数据库,所以我必须得到0或1填充该字段。

What do I have to do to get any of these values? I need it to send them later to the database, so I have to get 0 or 1 to fill that field.

推荐答案

你的输入的名称是分开的,它不会真正地作为单选按钮。尝试将名称更改为gender,并向其中添加适当的值。它可以为0为男性,1为女性,或只是男性和女性的术语。我使用后面的例子如下。控制器部分使用form_validation库,这在处理表单时非常有用。

Since the name of your input are separate, it won't really function as radio buttons. Try changing the name to something like "gender" and add appropriate values to it. It can be 0 for male, 1 for female, or just the term male and female. I used the latter for the example below. The controller section uses a form_validation library, which should be very useful when dealing with forms.

查看:

<input type="radio" name="gender" value="male">Male</input>
<input type="radio" name="gender" value="female">Female</input>

控制器:

    $this->load->library('form_validation');

    $this->form_validation->set_rules('gender', 'Gender', 'required'); // This will check if gender is selected.

    if ($this->form_validation->run()) {
      $gender = $this->input->post('gender'); // This will contain "male" or "female"
    }
    else {
      $error = validation_errors();
    }

这篇关于我如何得到的性别单选按钮(男性,女性)的值与CodeIgniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆