How to save Checkbox data based on click behaviour?

132 Views Asked by At

OK, I have a form where the data is persisted into database with create/edit. On edit page there is a checkbox with name audit_check which is clicked by a user and the logs should be generated.

if($form->isSubmitted()){
 if(user checks){
    //create logs
  }else{
   //do nothing
  }
 $em->persist($data);
 $em->flush();
}

Since the requirement of the task is to generate click logs and checkbox is rarely clicked while the pages is often edited. How to get the user click behavior such that when the user checks/uncheck the logs must be generated that the user clicked the checkbox.

1

There are 1 best solutions below

0
Diego D On

You could keep track of clicks made on such checkbox and submit that value together with the form data when it gets submitted. One way to achieve that would be having an hidden input like this:

<input type="hidden" id="clicksMade" name="" value="0" />

inside such form so that you'll have its value in the update action in the controller when the form gets submitted.

To update your click counter client side before the user submits the form (I used jQuery here I hope you don't mind):

$(document).ready( () => {
    $('#audit_check').on('change', () => {
      let i = parseInt( $('#clicksMade').val() );
      $('#clicksMade').val(++i);
    });
});