Page 1 of 1

catalog - user defined attribute as a checkbox

Posted: Wed Jul 19, 2006 9:14 pm
by mikemcvey
Hi!

I am trying to convert a user defined attributte in the Catalkog module to display as a check box but am having trouble...
My Attribute is named special offer and I an trying the code below

Code: Select all

if($safeattr=="specialoffer"){

if (strtolower(htmlspecialchars($this->GetPropertyValue($thisAttr),ENT_QUOTES))=="on"){
$checked= ' checked="true"';
} else{
	$checked= '';
}
 array_push($ret,array($thisAttr,'<input type="checkbox" name="specialoffer" '.$checked.'>'));
}			
This works in that it reads the value and changes the check box to true.. but when I uncheck and submit the values aren't updated.. it always stays as "on"... I am kind-off stumped and spent ages on trying to get ba simple check box updated!

Any ideas Greatly appreciated.

Re: catalog - user defined attribute as a checkbox

Posted: Wed Jul 19, 2006 9:29 pm
by mikemcvey
I should add that it works when I go from unchecked to checked.. the value changes to "on"..

But when I uncheck the value stays as "on"... I want it to be off!

Re: catalog - user defined attribute as a checkbox

Posted: Sat Jul 22, 2006 12:48 am
by sjg
The problem with checkboxes is that a form doesn't actually submit anything for an unchecked checkbox.

A trick I've learned from Ruby on Rails for this, is you have a hidden field with the name of the checkbox field and the value setting for "off" followed by your actual checkbox input.

Typically, if the field name lacks brackets, the second field with a given name will win out in the form submission, so this works even when they check the checkbox.

Try this:

Code: Select all

array_push($ret,array($thisAttr,'<input type="hidden" name="specialoffer" value="off"><input type="checkbox" name="specialoffer" '.$checked.'>'));
Good luck!
___Samuel___