0

Hey I'm trying to create a checkable option for whether to make a file downloadable or not:

<input type='checkbox' id='dTAG' name='dTAG[]' value='true'>

I have JS appending new checkboxes once the user chooses they want to add a file:

udInput = document.createElement('input');
udInput.setAttribute('type', 'checkbox');
udInput.setAttribute('name', 'dTAG[]');
udInput.setAttribute('value', 'true');

This all works ^, but my problem is when taken to PHP, it will only store the values of those true, and not all values. I've tested this by:

echo count($_POST['dTAG']);

and where as say a user has uploaded 3 files, checked 2 but left 1 checkbox unchecked.. the value returned for

echo count($_POST['dTAG']);

is:

2                           // not of 3 total checkboxes.

how do I get it to account for ALL checkbox values? whether it returns exactly false when unchecked doesn't matter as much.. I mostly just need the proper count of checkboxes, both checked and unchecked.

UPDATE: I got it working using Alan's method, thanks man!

Flame-7rb
  • 9
  • 2
  • I would count your checkboxes in your JavaScript and pass it to PHP via a hidden form field or insert into your ajax data depending on which method you are using. – Alan Dec 21 '21 at 20:25
  • I'll try starting new checkboxes as uncheck through js, have an onclick event for each one that checks/unchecks it, then store all those values in hidden field. – Flame-7rb Dec 21 '21 at 20:37
  • Right before you submit your form, you should be able to count them all. That should be simpler than having an onclick event just for counting. – Alan Dec 21 '21 at 20:42
  • 2
    To be clear, unchecked checkboxes are not sent in the form request at all, so it's not that "PHP doesn't" it's "the information is never sent to the server". So either PHP will need to have some kind of foreknowledge of all checkboxes to determine which/how many were unchecked, or you need to do that work in JS as Alan has suggested. – Sammitch Dec 21 '21 at 20:57
  • Does this answer your question? [POST unchecked HTML checkboxes](https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes) – Don't Panic Dec 21 '21 at 22:52

0 Answers0