lundi 11 octobre 2021

HTML - Submitting unchecked value for checkbox

I'm trying to get it so that the unchecked value for a checkbox is sent with a POST request. Research shows to use a hidden field with the same name.

The issue i've ran into is i'm submitting some fields as arrays (the reason for submitting the unchecked value in the first place) but when the value is checked its sending both the unchecked and checked value

It works fine if its not sent as an array, but as I'm dynamically adding fields giving them individual names is a pain.

I've currently got

<input type="hidden" value="0" id="vendorCompleted[]" name="vendorCompleted[]"><input type="checkbox" value="1" id="vendorCompleted[]" name="vendorCompleted[]">

And when i print the post request i get

[vendorCompleted] => Array
   (
       [0] => 0
       [1] => 1
   )

I add additional lines using

$( "#vAdd" ).click(function() {
            $('#vendor tr:last').before('<tr>' + 
                '<td width="10%"><input type="text" id="vendor[]" name="vendor[]"></td>' +
                '<td width="10%"><input type="text" id="vendorTicket[]" name="vendorTicket[]"></td>' +
                '<td width="10%"><input type="checkbox" id="vendorCompleted[]" name="vendorCompleted[]"></td>' +
                '<td><input type="hidden" value="0" id="vendorNotes[]" name="vendorNotes[]"><input type="text" value="1" id="vendorNotes[]" name="vendorNotes[]"></td>' +
            '</tr>');
        });



Aucun commentaire:

Enregistrer un commentaire