crmpicco
crmpicco

Reputation: 17181

detect if a checkbox array has been ticked with Smarty 2

Within my Smarty template I have a series of checkboxes that are named "region_codes", which contain a value of "GB", "US", "EU" etc, etc...

This is the code in my Smarty template:

<input type="checkbox" name="region_codes[]" value="{$region_code}" {if isset($smarty.request.region_codes.$region_code)}checked="yes"{/if} />

As you can see from the code snippet, I have attempted to detect whether the a checkbox has been ticked when the form is submitted. How is this done in Smarty, as the above code does not work. It doesn't throw an error, but it does not see the submitted value.

EDIT: When I print out the region_codes parameter that is coming through request from within my Smarty template I get this:

[region_codes] => Array ( [0] => EU [1] => RW )

Upvotes: 0

Views: 2173

Answers (2)

heyanshukla
heyanshukla

Reputation: 669

{if $region_code|in_array:$smarty.request.region_codes}checked="yes"{/if}

Upvotes: 3

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

Change the condition to

{if $smarty.request.region_codes|in_array:$region_code}checked="yes"{/if}

Upvotes: 1

Related Questions