catalog - user defined attribute as a checkbox

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
mikemcvey
Forum Members
Forum Members
Posts: 61
Joined: Tue May 02, 2006 4:08 am

catalog - user defined attribute as a checkbox

Post 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.
Last edited by mikemcvey on Wed Jul 19, 2006 9:30 pm, edited 1 time in total.
mikemcvey
Forum Members
Forum Members
Posts: 61
Joined: Tue May 02, 2006 4:08 am

Re: catalog - user defined attribute as a checkbox

Post 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!
User avatar
sjg
Power Poster
Power Poster
Posts: 310
Joined: Thu Jan 27, 2005 5:11 pm
Location: Los Angeles, CA

Re: catalog - user defined attribute as a checkbox

Post 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___
Many modules available from the http://dev.cmsmadesimple.org
The CMS Made Simple Developer Cookbook is now available from Packt Publishers!
Post Reply

Return to “Developers Discussion”