Welcome Guest, Not a member yet? Register   Sign In
unchecked form_checkbox does not post it's variable
#1

[eluser]Jow272[/eluser]
Hello all,

I'm having this weird problem. I'm using a single form_checkbox in a view but it only returns it's variable when it is checked.

The code:

in the view:
Code:
echo form_checkbox('restricted_cbx', 'R', !isset($restricted) ?
   set_value('restricted_cbx') : $restricted) . $this->lang->line('view_restricted');

how it's value is retrieved in the controller:
Code:
$value = ($_POST['restricted_cbx'] == 'R') ? 'R' : 'P';

In all cases the checkbox is displayed in the view. If I check it and send the page everything is ok and the value is correct. But if I leave the checkbox unchecked I get a warning after sending the page that states "Undefined index: restreicted_cbx".

Can anyone tell me what I'm doing wrong?

Thanx in advance.

Michel
#2

[eluser]Jow272[/eluser]
By the way, I even tried it like
Code:
echo form_checkbox('restricted_cbx', 'R')
with the same result. I now resolved the issue with doing
Code:
$value = (isset($_POST['restricted_cbx']) && $_POST['restricted_cbx']) ? 'R' : 'P';
but that's offcourse not the way to go.
#3

[eluser]Colin Williams[/eluser]
That's why you should use $this->input->post(), as it avoids these warnings. And FWIW, the checkbox is working as it should. You just need to adjust your controller code.
#4

[eluser]Jow272[/eluser]
Thanx Colin,

This indeed works. I altered the code. But I keep wondering why it is like this. It seems strange to me that a form controller that is displayed correctly and has a name, does not return a variable.

But anyway I will alter the $_POST usage throughout my code.

Best,

Michel
#5

[eluser]Colin Williams[/eluser]
That's just how the browser handles it. The problem is that there's no standard for what an unchecked checkbox value would be. Should it be 0, or NULL, or ''? What if the value of the checkbox was explicitly 0 (which is not at all uncommon)? See where the breakdown is?

The HTML spec just says for it not to exist to avoid the whole issue.




Theme © iAndrew 2016 - Forum software by © MyBB