Send Uniform checkboxes data to url

I have an uniform form with only checkboxes. When I submit the form i can get the data from for $form variable but the url ramains unchanged. I would link to have the data in the url so I can use it as link in others parts of the site. Any suggestions?

Controller

<?php

use Uniform\Form;

return function ($kirby, $page) {

$productos = $page->children()->listed()->sortBy('created', 'desc', 'time', 'asc');

  $form = new Form([
    'qCategoria' => [],
    'qForma' => []
  ]);

// paginación
$productos = $productos->paginate(24);

// paginación shortcut
$paginacion = $productos->pagination();

return [
    'productos' => $productos,
    'paginacion' => $paginacion,
    'form' => $form
    ];
};

Template

       <form  method="POST">            
         <div class="col">
            <?php $valueCategoria = $form->data('qCategoria') !== '' ? $form->data('qCategoria') : [] ?>
            <?php foreach( $site->categorias()->toStructure() as $categoria ): ?>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="qCategoria[]" value="<?= $categoria->categoria() ?>" id="qCategoria<?= $categoria ?>" <?= in_array($categoria->categoria(), $valueCategoria)? 'checked':'' ?>>
              <label class="form-check-label" for="qCategoria<?= $categoria ?>"><?= $categoria->categoria() ?></label>
            </div>
            <?php endforeach ?>
          </div>

          <div class="cols">
            <?php $valueForma = $form->data('qForma') !== '' ? $form->data('qForma') : [] ?>
            <?php foreach( $site->formas()->toStructure() as $forma ): ?>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="qForma[]" value="<?= $forma->forma() ?>" id="qForma<?= $forma ?>" <?= in_array($forma->forma(), $valueForma)? 'checked':'' ?>>
              <label class="form-check-label" for="qForma<?= $forma ?>"><?= $forma->forma() ?></label>
            </div>
            <?php endforeach ?>
          </div>
       </form>

When you use a POST request, the URL does not change. If you want the URL to change, use a GET request.

I’m trying to follow this example Kirby Uniform Plugin: Multiple Checkbox but $form->old(‘qCategoria’) won’t give me any results

The value of a checkbox only gets sent by an HTML form if the checkbox is checked. When a checkbox is unchecked, no value at all gets sent - as if the checkbox wasn’t present in the form at all. That is how checkboxes are supposed to behave in HTML. You can read all about it here:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox

If you want to have different content shown to the user based on your URL, it might be easier to do this with Routes.

changing to GET now gives me the url, i can get the variables with get()in the template but the $form object it’s empty so I can’t use the variables like $form->old('qCategoria')

If you have a form with only checkboxes, and all checkboxes are unchecked, the form will be empty.

As explained above, a checkbox is not like a text field. When you have a text field in your form, if the field is left empty, an ‘empty’ value still gets submitted for the field. With a checkbox, when the checkbox is unchecked, the form behaves as if the checkbox didn’t exist, and nothing gets sent to the server. You don’t get a “checkbox=unchecked” - you get no mention of the checkbox at all.

I do check the checkboxes, the url gets the data but the $form created on the controller doesn’t, so I can’t use Uniform’s methods to check what was submitted.

Sorry if i’m not being clear.

I’m tryin to get my code to the minimum necessary, maybe it will help to find the issue. Maybe i should stick to get()? It’s just that uniform should work…

template:

<?php dump($form->data()) ?>
/* Array
(
    [qCategoria] => 
    [qForma] => 
    [qMontura] => 
)*/
<?php dump(get('qCategoria')) ?>
  /*
  Array
  (
  [0] => Hombre
  [1] => Mujer
  )
*/
<form method="GET">
        
<input type="checkbox" name="qCategoria[]" value="Hombre" id="qCategoria1">
<label for="qCategoria1">Hombre</label>
              
<input type="checkbox" name="qCategoria[]" value="Mujer" id="qCategoria2">
<label for="qCategoria2">Mujer</label>
          
<?php echo csrf_field(); ?>
<?php echo honeypot_field(); ?>
<input class="btn btn-primary font-weight-bold" type="submit" value="Filtrar">

</form>

Controller

<?php

use Uniform\Form;

return function ($kirby, $page) {

  // todos los articulos
  $productos = $page->children()->listed()->sortBy('created', 'desc', 'time', 'asc');

  $form = new Form([
    'qCategoria' => [],
    'qForma' => [],
    'qMontura' => []
  ]);
  
  // paginación
  $productos = $productos->paginate(24);

  // paginación shortcut
  $paginacion = $productos->pagination();

  return [
    'productos' => $productos,
    'paginacion' => $paginacion,
    'form' => $form
  ];
};

Actually, I don’t see the purpose of the Uniform plugin here; all you want seems to be a get request to filterr some data? You don’t need a token or anything for that.

1 Like

It was going to be different at first but they keep changing the requirements :sweat_smile: I was thinking the same, that it’s no longer necessary to use Uniform.