PHP无法接收表单提交数据问题排查求助
Let's get straight to the root of the problem: your form's input naming convention doesn't match how your PHP code is trying to access the submitted data. That's why all checkboxes are defaulting to 'N' and all text inputs are ending up as 'nullo'—your PHP can't find the data it's looking for!
Key Mismatches Between Form and PHP Code
1. Checkbox Name vs. PHP Access Logic
Your form uses array-style naming for checkboxes:
<input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[]" value="check"/>
But your PHP code is trying to access individual named fields like $_POST['L_checkb_'.$counter]—this doesn't exist! When you use [] in a form input name, PHP parses it as an array ($_POST['L_chckb_']) containing only the checked checkboxes. Worse, without explicit indexes in the name, you can't map the submitted values back to their original 0-6 numbering.
2. Text Input Name vs. PHP Access Logic
Same issue applies to your text inputs:
<input type="text" id="Limpeza0" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
Your PHP looks for $_POST['Limpeza'.$i], but the actual submitted data lives in $_POST['Limpeza'] (an array), so those individual keys don't exist—hence empty() always returns true, setting everything to 'nullo'.
Corrected Solution
Step 1: Update Form Input Names (Critical for Matching)
First, modify your checkboxes and text inputs to use indexed array names so you can map each input to its correct position:
<!-- Checkboxes (0 to 6) --> <input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[0]" value="check"/> <input type="checkbox" class="x" id="L_chck_1" name="L_chckb_[1]" value="check"/> <!-- ... repeat up to L_chck_6 --> <!-- Text inputs (0 to 4) --> <input type="text" id="Limpeza0" name="Limpeza[0]" class="listInput" autocomplete="off" maxlength="50"> <input type="text" id="Limpeza1" name="Limpeza[1]" class="listInput" autocomplete="off" maxlength="50"> <!-- ... repeat up to Limpeza4 -->
This ensures each input maps to a specific index in the submitted array, even if the checkbox is unchecked (unchecked checkboxes won't be submitted, but we'll handle that in PHP).
Step 2: Revised PHP Processing Code
Now update your PHP to correctly read the indexed arrays, set defaults, and process values:
if (isset($_POST['submitButton'])){ // Initialize checkbox array with default 'N' for all 7 positions (0-6) $L_chck = array_fill(0, 7, 'N'); // Update checkboxes that were actually checked if (isset($_POST['L_chckb_']) && is_array($_POST['L_chckb_'])) { foreach ($_POST['L_chckb_'] as $index => $value) { // Ensure we only update valid positions (0-6) if ($index >= 0 && $index <= 6) { $L_chck[$index] = 'Y'; } } } // Initialize text input array with default 'nullo' for all 5 positions (0-4) $Limpeza = array_fill(0, 5, 'nullo'); // Process text input values if (isset($_POST['Limpeza']) && is_array($_POST['Limpeza'])) { foreach ($_POST['Limpeza'] as $index => $value) { if ($index >= 0 && $index <= 4) { $trimmedValue = trim($value); // Set to 'nullo' only if the input is truly empty (after trimming whitespace) $Limpeza[$index] = empty($trimmedValue) ? 'nullo' : $trimmedValue; } } } // Repeat similar logic for other input types (Outros, etc.) // Pass the processed arrays to your save function savingListData($L_chck, $Limpeza, ...); }
Why This Works
- We start with default values for all positions, so even unsubmitted (unchecked/empty) inputs get the correct fallback value.
- By using indexed array names in the form, we can directly map submitted values back to their original positions.
- We add checks to ensure we only process valid indexes, preventing unexpected errors if extra data is submitted.
内容的提问来源于stack exchange,提问作者Incomplentence

