vendredi 4 octobre 2019

How to detect if any checkbox is unchecked in case of multiple checkboxes with same name

I need to detect at ajax post submitting if there is at least one unchecked checkbox from a list of several ones with the same name and different values.

<input type="checkbox" name="product_id[]" value="<?php echo $product->id;?>" checked>

In case the user had unchecked at least one checkbox a flag variable should be set to 1 and sent to the server. I found several examples about how to detect the state of a specific checkbox, but how could I parse all of them to verify if at least one has been unchecked before submitting the form?

Finally, how could I add to ajax post this further flag variable, let's say uncecked_boxes=1; if I use the form.serialize() function to populate the data field?

$("#desktop_new_alarm_research").submit(function(e) {

    e.preventDefault(); // avoid to execute the actual submit of the form.

    var form = $(this);
    var url = ajax_url+'Login/getResearchResultsForNewPriceAlarms';

    $.ajax({
       type: "POST",
       url: url,
       data: form.serialize(), // serializes the form's elements.
       success: function(data)
       {
           $('#research_results_container').html(data);
           alert(data); // show response from the php script.
       }
     });


});



Aucun commentaire:

Enregistrer un commentaire