12

I am new to PHP codeigniter,

how to get checkbox values using php Codeigniter in Controller.

Here is the Checkboxes, i want to get checkbox values base on name as "businessType" in controller using post menthod.

<input type="checkbox"name="businessType" value="1">
<input type="checkbox"name="businessType" value="2">
<input type="checkbox"name="businessType" value="3">

Please suggest

thanks

tpae
  • 6,286
  • 2
  • 37
  • 64
Vicky
  • 9,515
  • 16
  • 71
  • 88

4 Answers4

44
<input type="checkbox" name="businessType[]" value="1">
<input type="checkbox" name="businessType[]" value="2">
<input type="checkbox" name="businessType[]" value="3">

do $data = $this->input->post('businessType');

You should see that $data is an array, and shows different values. Try doing var_dump($data); to see what's inside the array.

var_dump()

tpae
  • 6,286
  • 2
  • 37
  • 64
9

Put braces after each name. Give each a unique value:

<input type="radio" name="businessType[]" value="1">
<input type="radio" name="businessType[]" value="2">
<input type="radio" name="businessType[]" value="3">

Get them like this:

substr(implode(', ', $this->input->post('businessType')), 0)
Kenzo
  • 3,513
  • 4
  • 17
  • 16
7

If only one of these checkboxes can be selected at a time, you should use a group of radio buttons (type="radio") instead. I assume this is what you are trying to do since the names of all of the inputs are the same.

To get the value of the checkbox or radio button group, use:

$this->input->post('businessType')

Edit:

If you are actually wanting checkboxes, you will need to name them all something different:

<input type="checkbox"name="businessType1" value="1">
<input type="checkbox"name="businessType2" value="2">
<input type="checkbox"name="businessType3" value="3">

And then use the same post method as before:

$this->input->post('businessType1') //the first checkbox's value
$this->input->post('businessType2') //the second
$this->input->post('businessType3') //the third
davidscolgan
  • 7,508
  • 9
  • 59
  • 78
  • but with the help of this how will i get multiple selection values? – Vicky Nov 08 '10 at 10:24
  • Ah, if you do actually want to get multiple values, you will need to have a different name for each checkbox. See above. – davidscolgan Nov 08 '10 at 12:30
  • this method will make it pretty difficult to work with checkboxes that are dynamically generated. Why not use method suggested by @tpae ? – aphoe Oct 23 '14 at 23:21
-1

As dvcolgan (+1) suggested, radio buttons are what you should use, here is an example wrapped in a fieldset.

Your HTML

<fieldset>
<legend>Choose Business Type:</legend><br>
<input type="radio" name="businessType" value="1">
<input type="radio" name="businessType" value="2">
<input type="radio" name="businessType" value="3">
</fieldset>

Then in your php

$businessType = $this->input->post("businessType");
Bella
  • 3,209
  • 23
  • 26