Forum
Checking checkbox in form based on variable
Hi,
Feel like I am close with this one but can't quite make it work. I have an array generated from query string which is from selected options on a form. The form looks like this:
<perch:before>
<form class="filter-list" method="get">
</perch:before>
<label for="track-<perch:events id="categorySlug" />"><input type="checkbox" id="track-<perch:events id="categorySlug" />" name="track[]" value="<perch:events id="categorySlug" />" /><perch:events id="categoryTitle" /></label>
<perch:after>
<div>
<input type="submit" value="Submit" />
</div>
</form>
</perch:after>
And is on the page like this:
perch_events_categories([
'template' => 'category_link_checkboxes'
]);
An example of the array looks like this (though could have more or less items depending on what is selected):
Array
(
[event-category-one] => checked
[event-category-two] => checked
)
The key here is the same as the slug for the category that has been selected.
I am using that to set some variables in Perch:
foreach ($array as $key => $value) {
PerchSystem::set_var($key, $value);
}
All fine so far. Now I am trying to use those variables to check the checkboxes in the form, so when the page reloads after submit the checkboxes stay checked. It is an on-page filter so this shows the user what is being filtered. I have tried this with perch:if
:
<perch:before>
<form class="filter-list" method="get">
</perch:before>
<label for="track-<perch:events id="categorySlug" />"><input type="checkbox" id="track-<perch:events id="categorySlug" />" name="track[]" value="<perch:events id="categorySlug" />" <perch:if exists="<perch:events id="categorySlug" />">checked="checked"</perch:if> /><perch:events id="categoryTitle" /></label>
<perch:after>
<div>
<input type="submit" value="Submit" />
</div>
</form>
</perch:after>
But it isn't working. No PHP errors but no checkboxes are checked. If I hard code it (e.g. perch:if exists="event-category-one"
) it works fine. In showall
the variables are there as I would expect.
Any thoughts on how I can get this working? Not tied to my method if there is a better way.
This is only from memory, but I'm fairly sure you're not allowed to do
This is probably proven by your test with a hard-coded value for
exists
. Dynamically putting a value in there isn't supported.Rats, that's a pain. Does anyone know if there is another way I can do this?
A load of <perch:if> fields is probably one way.
OR, I wonder if template filters in Perch 3 could help - quoting the blog post: "For example, we created a quick filter that sums the values of numbers in other fields and displays the result."
If you can look at other fields in a filtered field, could that be the answer?
Ok I have sorted it by doing a bit of a switcheroo. For future reference:
Before messing around with it my
GET
array looked like this:I used
implode
on this to get a comma-separated string of the selected categories, and set these to a Perch variable calledchecked
.Now I can do this in the template:
Using curly brackets for a dynamic value. Phew.
Excellent - good solution. Duly noted!