Page 1 of 1

[Solved] Checkbox only in params if checked

Posted: Tue Dec 30, 2008 9:41 pm
by tmk
I'm creating a checkbox and assigning it to a smarty variable, outputted by a template.

$s = $this->CreateInputCheckbox($id,'mycheckbox' ,'1',  $checkboxvalue );
$smarty->assign('input_mycheckbox',$s);

The checkbox is on the form, but only if the checkbox is checked, does it get returned in the $params array when the form is submitted.

Using the php:

print_r($params);

I don't get mycheckbox => anything  if not checked.

It is not in the list of $params elements, there is no $params['mycheckbox']

Am I doing something wrong?

Re: Checkbox only in params if checked

Posted: Tue Jan 06, 2009 12:02 pm
by irish
tmk,

You are not doing anything wrong. That is basic functionality of a checkbox, if it's not checked then it is not passed via POST.

If you need to know if the checkbox is selected, you could use:

Code: Select all

$mycheckboxvalue = $params["mycheckbox"]?$params["mycheckbox"]:0;

The code above will either assign the value of the checkbox to the variable if checked or else assign 0 to the variable.

Code: Select all

if($mycheckboxvalue){
   echo "mycheckbox has been selected";
}else{
  echo "mycheckbox has NOT been selected";
}