how can i get multiple checked box values in codeigniter with this code
<input type="checkbox" name="assign[]" value="Keyur"> Keyur<br/>
<input type="checkbox" name="assign[]" value="Ritesh"> Ritesh<br/>
<input type="checkbox" name="assign[]" value="Saurabh"> Saurabh<br/>
<input type="checkbox" name="assign[]" value="Maulik"> Maulik<br/>
at the controller
$data1 = $this->input->post('assign[]');
i do that but can't get values,where i make mistake????
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.
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.
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 />";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With