Forum

Thread tagged as: Question, Events

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.

Mike Harrison

Mike Harrison 37 points

  • 4 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

This is only from memory, but I'm fairly sure you're not allowed to do

<perch:if exists="<perch:events id="categorySlug" />">

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?

Duncan Revell

Duncan Revell 78 points
Registered Developer

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:

Array
(
    [0] => event-category-one
    [1] => event-category-two
    [2] => event-category-three
)

I used implode on this to get a comma-separated string of the selected categories, and set these to a Perch variable called checked.

Now I can do this in the template:

<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 id="checked" match="contains" value="{categorySlug}">checked</perch:if> />
<perch:events id="categoryTitle" />
</label>

Using curly brackets for a dynamic value. Phew.

Duncan Revell

Duncan Revell 78 points
Registered Developer

Excellent - good solution. Duly noted!