简体   繁体   English

获取复选框的多个值

[英]get multiple values of checkbox

how can i get multiple checked box values in codeigniter with this code如何使用此代码在 codeigniter 中获取多个复选框值

<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/>

at the controller在 controller

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

i do that but can't get values,where i make mistake????我这样做但无法获得价值,我在哪里犯错????

Use this:用这个:

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

It will be an array, the same thing as $_POST['assign'] .它将是一个数组,与$_POST['assign']相同。

Example:例子:

// 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}.";
}

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() .不幸的是,如果您需要访问特定索引,则必须先将其分配给变量或使用$_POST而不是$this->input->post() Example:例子:

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

Update : As of PHP 5.4, you can access the index right from the function call like this:更新:从 PHP 5.4 开始,您可以从 function 调用中访问索引,如下所示:

$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).无论哪种方式,请确保在您尝试访问它之前设置发布数据索引(如果您需要这样做)。

Try this one, in your controller:试试这个,在你的 controller 中:

$data1 = $this->input->post('assign'); //this returns an array so use foreach to extract data

foreach( $data1 as $key => $value){

       echo $value.' '."</br>";

}

I have done this to my program and it worked.我已经对我的程序做了这个并且它有效。

try this:尝试这个:

for($i = 0; $i< count($_POST['assign']); $i++){
    echo $_POST['assign'][$i] . "<br />";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM