Jump to content

[SOLVED] Chechbox group: How to show selected options on frontend form


jacmaes
 Share

Recommended Posts

I'm sure it's been asked before on the forums, but I haven't found a relevant answer after multiple searches.  I have a form on the frontend that mirrors the user profile on the backend. After logging in, I want the user to be able to edit multiple fields on his or her profile. For simple text entries (e.g. First Name, Street Address, Phone Number or Email), and single options (e.g. radio buttons for Gender: male or female or select dropdowns for a Country list), I have no issue and everything works fine: the user first fills out the form then can go back and update whichever field they want. That's standard profile editing stuff from the frontend.

But I hit a roadblock when I want to offer multiple choices. For example, I have a Select Options field set up to show multiple checkboxes for choosing various types of driver's licences. With the following code (edited for brevity):

    $licence_types = array('B1', 'B', 'A1', 'C1', 'D1'); // same options as defined in my select options field
          foreach($licence_types as $licence) {
          $content .= "<label><input type='checkbox' name='licences[]' value='$product'> $licence</label>";
        }

....
  $user->of(false);
  $user->licences = $licences;
  $user->save();
  $user->of(true);

the user can successfully submit this info from the frontend form:

drivers-licence.png.6acbb42c1ad538ba646d4ba38c4f6a4b.png

But now how do I show them the result  of what they submitted when they go back and want to update their profile from the same form? In other words, how do I insert the checked='checked' (or simply 'checked') HTML in the previously selected checkboxes?  For example, if they selected the first two options –B1 and B– in the example above, how do I make these two checkboxes ticked?

With the following loop, I can show the previously selected checkboxes:

foreach($user->licences as $licence) {
            $content .= "<label for='$licence->id'> <input name='licences[]' type='checkbox' id='$licence->id' value='$licence->id' checked>$licence->title</label>";
          }

 

How do I merge the two loops so that they see this, as expected:

drivers-licence2.png.3c8f19b4554f38fa6b37c6ed56308909.png

 

Maybe with in_array()? Any help would be greatly appreciated.

 

 

Link to comment
Share on other sites

Untested, quickly thought through, blah blah..

From the docs you linked to: Under Manipulating options on a page from the API, see Getting all possible options

// @note: forget this code!
// get the Select Options Field
$field = $fields->get('licences');
// get all the defined options
$all_options = $field->type->getOptions($field);

foreach($user->licences as $licence) {
    // check 'checked' status
    $checked = $all_options->get("id={$licence->id}") ? ' checked': '';// untested!
    $content .= "<label for='$licence->id'> <input name='licences[]' type='checkbox' id='$licence->id' value='$licence->id' $checked>$licence->title</label>";
}

 

Edit:

I am not sure my logic above is correct but I have to run. It might be you need to loop through the $all_options instead. I'll check back later..., sorry

Try this one instead. @note: incomplete, but gives you an idea

// get the IDs of selected options @note: untested! + pseudo-code
$selectedOptions = getArray($user->licences);// untested! pseudo-code; Here, manipulate to get IDs!
// @see: https://processwire.com/api/ref/wire-array/

foreach($all_options as $option) {
    // check 'checked' status
    $checked =  in_array($option->id, $selectedOptions) ? ' checked': '';// untested!    
    $content .= "<label for='$licence->id'> <input name='licences[]' type='checkbox' id='$licence->id' value='$licence->id' $checked>$licence->title</label>";
}

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Thanks a million, @kongondo ! You pointed me to the right direction. In the end I had to loop through the $all_options as you anticipated. The following works:

  // get the Select Options Field
          $field = $fields->get('licences');
          // get all the defined options
          $all_options = $field->type->getOptions($field);

          foreach($all_options as $licence) {
              // check 'checked' status
              $checked = $user->licences->get("id={$licence->id}") ? ' checked': '';
              $my_form .= "<label for='$licence->id'> <input name='carnet_de_conducir[]' type='checkbox' id='$licence->id' value='$licence->id' $checked>$licence->title</label>";
          }

After hours of wasted sleep and overengineered failed attempts, here you come to save the day with this classically elegant and readable PW snippet. I'm so grateful ! Gottta love this forum.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...