Yettt
Yettt

Reputation: 31

Codeigniter Getting the value of a checkbox

I want to get the value of a checkbox once it checked.

From my code

$vat_checkbox = $this->input->post('vat_checkbox');

print_r($vat_checkbox);
die();

it display "on". I will use it for validation. What would be the proper way without using javascript?

Upvotes: 0

Views: 47

Answers (2)

M.Hemant
M.Hemant

Reputation: 2355

Just add value attribute in input tag if you dont want to do with jquery

<form action="test.php" method="POST">
    <input type="checkbox" name="vat_checkbox_1" value="1" checked>
    <input type="checkbox" name="vat_checkbox_2" checked>
    <input type="submit" name="submit" value="SUBMIT">
</form>

If you added value then it will give you value from value attribute else "on"
Output:

Array
(
    [vat_checkbox_1] => 1
    [vat_checkbox_2] => on
    [submit] => SUBMIT
)

Here, In Controller you can directly use your checkbox data without any manipulation.

Upvotes: 0

Devsi Odedra
Devsi Odedra

Reputation: 5322

When checkbox is checked you will get value as on else you will not get that tag also. so You can do something like this.

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

if(isset($post['vat_checkbox']) && $post['vat_checkbox'] == 'on') {
     $post['vat_checkbox'] = 1;
} else {
     $post['vat_checkbox'] = 0;
}

Here $post['vat_checkbox'] you can use for operation

Upvotes: 1

Related Questions