Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 337
keyur Avatar asked Jun 04 '11 05:06

keyur


People also ask

How can I get multiple checkbox values in jQuery?

Using jQuery, we first set an onclick event on the button after the document is loaded. In the onclick event, we created a function in which we first declared an array named arr. After that, we used a query selector to select all the selected checkboxes. Finally, we print all the vales in an alert box.

Can checkbox have two values?

A checkbox can pass one of the two values to this variable - one value for the checked state and another one for the unchecked state.


3 Answers

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

like image 154
Wesley Murch Avatar answered Sep 23 '22 19:09

Wesley Murch


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.

like image 37
janine kate Avatar answered Sep 24 '22 19:09

janine kate


try this:

for($i = 0; $i< count($_POST['assign']); $i++){
    echo $_POST['assign'][$i] . "<br />";
}
like image 43
Core Avatar answered Sep 23 '22 19:09

Core