How do I link the file input type with the checkbox?
This is what I have so far:
<input type="file" name="file_upload[0]" /> <input type="checkbox" name="checkbox[0]"> checkbox 1 <br />
<input type="file" name="file_upload[1]" /> <input type="checkbox" name="checkbox[1]"> checkbox 2 <br />
<input type="file" name="file_upload[2]" /> <input type="checkbox" name="checkbox[2]"> checkbox 3 <br />
<input type="file" name="file_upload[3]" /> <input type="checkbox" name="checkbox[3]"> checkbox 4 <br />
Now I need to check in PHP when a file is uploaded the correspondent checkbox is checked (preferably with a loop), but I can’t make it work.
This is what I have in PHP:
if (isset($_FILES['file_upload[1]']) && isset($_FILES['checkbox[1]'])) {
//do something
} else {
//do something else
}
You need to change your checkbox input tag to
<input type="checkbox" name="checkbox[3]" value="1" >i.e. you need to add missing
value=""attributeand as mentioned in the comment
you need to check
if(isset($_POST['checkbox'][0])) {instead in$_FILESTry printing both
print_r($_FILES)and print_r($_POST)… this should give you idea of what structure of data you are receiving after submittingYou can just loop through the file_uploaded array and then check if related checbox index is set.