php 如何使用 CodeIgniter 获取性别单选按钮(男性、女性)的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13618792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:47:14  来源:igfitidea点击:

How do I get the value of gender radio button (male, female) with CodeIgniter?

phphtmlcodeigniterweb

提问by user1822263

I got this in my view.

我在我看来是这样的。

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

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.

我该怎么做才能获得这些值中的任何一个?我需要它稍后将它们发送到数据库,所以我必须得到 0 或 1 来填充该字段。

回答by user1418338

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.

由于您输入的名称是独立的,因此它不会真正用作单选按钮。尝试将名称更改为“性别”之类的名称并为其添加适当的值。它可以是 0 代表男性,1 代表女性,或者只是男性和女性这个词。我在下面的例子中使用了后者。控制器部分使用了一个 form_validation 库,它在处理表单时应该非常有用。

View:

看法:

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

Controller:

控制器:

    $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();
    }

回答by brenjt

Make the names of both radio buttons the same. Example gender. In your controller do $this->input->post('gender');to get the value

使两个单选按钮的名称相同。例子gender。在您的控制器中执行$this->input->post('gender');以获取值

回答by user5982856

<div class="mtm _5wa2 _5dbb" id="u_0_f">
    <span class="_5k_3" data-type="radio" data-name="gender_wrapper" id="u_0_g">
    <span class="_5k_2 _5dba">
        <input type="radio" name="sex" value="1" id="u_0_d">
        <label class="_58mt" for="u_0_d">Female</label>
    </span>
    <span class="_5k_2 _5dba">
        <input type="radio" name="sex" value="2" id="u_0_e">
        <label class="_58mt" for="u_0_e">Male</label>
    </span>
    </span>
    <i class="_5dbc _5k_6 img sp_FbRIIaK1AuN sx_ebf223"></i>
    <i class="_5dbd _5k_7 img sp_FbRIIaK1AuN sx_1e1628"></i>
</div>

You need to edit custom.csslayout.cssand a few others just go to your site reg page and press ctrl shift I and look at the locations of code

您需要编辑custom.csslayout.css,其他一些只需转到您的网站注册页面并按 ctrl shift I 并查看代码的位置