获取复选框的多个值 [英] get multiple values of checkbox

查看:82
本文介绍了获取复选框的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用此代码在codeigniter中获得多个复选框值

how can i get multiple checked box values in codeigniter with this code

<input type="checkbox" name="assign[]" value="Keyur">&nbsp;Keyur<br/>
        <input type="checkbox" name="assign[]" value="Ritesh">&nbsp;Ritesh<br/>
        <input type="checkbox" name="assign[]" value="Saurabh">&nbsp;Saurabh<br/>
        <input type="checkbox" name="assign[]" value="Maulik">&nbsp;Maulik<br/>

在控制器上

$data1 = $this->input->post('assign[]');

我这样做但是无法获取值,而我却犯了错误???

i do that but can't get values,where i make mistake????

推荐答案

使用此:

$this->input->post('assign');

它将是一个数组,与 $ _ POST ['assign ']

示例:

// This assumes we know the post key is set and is an array,
// but you should definitely check first
foreach ($this->input->post('assign') as $key => $value)
{
    echo "Index {$key}'s value is {$value}.";
}

不幸的是,如果您需要访问特定的索引,则必须首先将其分配给变量,或使用 $ _ POST 代替 $ this-> input-> post() 。示例:

Unfortunately, if you need to access a specific index, you'll have to assign it to a variable first or use $_POST instead of $this->input->post(). Example:

$assign = $this->input->post('assign');
echo $assign[0]; // First value
echo $_POST['assign'][0]; // First value

更新:从PHP 5.4开始,您可以访问像这样的函数调用中的索引:

Update: As of PHP 5.4, you can access the index right from the function call like this:

$this->input->post('assign')[0];

不是推荐或更好的选择,只是为了让您知道这是可能的。

Not that it's recommended or better, but just so you know it's possible.

无论哪种方式,在尝试访问它们之前,请确保已设置帖子数据如果,则需要这样做方式)。

Either way, make sure the post data and the index is set before you try to access it (if you need to do so this way).

这篇关于获取复选框的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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