简体   繁体   中英

get multiple values of checkbox

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

at the 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'] .

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() . 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:

$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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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