How to submit multiple array checkbox with html forms

How to submit multiple array checkbox with html forms

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding Multiple Array Submissions in HTML Forms

When building inventory management systems or similar applications, it is common to need to process multiple distinct sets of data simultaneously based on user selection. A frequent stumbling block arises when trying to pass two separate arrays from an HTML form to a PHP backend: one array containing identifiers (like ISBNs) and another containing associated attributes (like book conditions). If the second array arrives empty or malformed, your logic fails silently, leading to incomplete data processing.

The core issue in scenarios like this usually stems from how $_POST handles arrays versus individual values, and specifically, how you are naming your inputs. In your example, you have an input named add[]. When PHP receives a POST request with multiple checkboxes sharing the same name ending in [], it automatically aggregates them into a single indexed array.

However, the specific problem described—where one array is processed but the other is not—often indicates a mismatch between what you expect to receive and what actually arrives. In your provided code, the form loops through catalog items and generates an input like:

<input type='checkbox' name='add[]' value='100001_used_' />

Notice the trailing underscore in the value string before the closing quote. This creates a parsing issue when you attempt to split that value later using explode("_", $checkbox). If your value is '100001_used_', splitting by _ results in three parts: ['100001', 'used', '']. The third element is an empty string, not a valid sub-condition.

Furthermore, the PHP function Inventory() checks for $_POST['add']. While this works if the form submits at least one checkbox with that name, relying on isset without ensuring the structure is exactly as expected can lead to logic errors if other fields are also named similarly or if the array structure differs.

To solve this and achieve your desired output where every submission includes both the ISBN and the condition, you must ensure that the data being sent matches the keys you are trying to read.

Correcting the Value Structure

The most direct fix involves cleaning up the value generation in the HTML loop. Instead of concatenating strings with trailing underscores that rely on explode, consider passing the components as distinct segments or using a delimiter that won't conflict with your data.

If you stick with a single string value, ensure the split logic accounts for empty strings resulting from trailing delimiters. For instance:

// In your PHP processing function
$temp = explode("_", $checkbox);

// Ensure we don't have an empty last element causing issues
$parts = array_filter($temp); // Removes empty strings if any

$arr[] = array(
   "isbn"       => $temp[0],
   "condition"  => $temp[1] ?? 'unknown', // Handle cases where split might fail
   "sub_condition" => isset($temp[2]) ? $temp[2] : ''
);

However, a cleaner architectural approach is to avoid relying on string splitting for complex data. Instead, structure your value string so the segments are clearly defined without ambiguity. If you are generating values like isbn_condition, ensure you aren't appending empty strings at the end of the string literal.

Handling Multiple Arrays Properly

Your goal is to pass two arrays: one of items selected for deletion (delete[]) and one of items selected for addition (add[]). Both can coexist in the same form submission without conflict, provided their names are distinct (e.g., delete[] vs add[]).

In your current setup, you are successfully capturing delete[]. The failure to process condition inside the add[] payload is strictly a data parsing issue within the loop. If explode("_", $checkbox) returns fewer than three elements, accessing $temp[2] will result in a notice or an empty value, which breaks your conditional logic expecting sub_condition.

To prevent this, validate the number of parts before accessing array indices. Additionally, ensure that your form action and method are consistent across all inputs.

// Safer parsing approach
if (is_array($_POST['add'])) {
    foreach ($_POST['add'] as $checkbox) {
        $parts = explode("_", $checkbox);
        
        // Only process if we have at least the ISBN and Condition
        if (count($parts) >= 2) {
            $arr[] = array(
                "isbn"       => $parts[0],
                "condition"  => $parts[1]
                // Remove sub_condition if it's empty or not present to avoid logic errors
            );
        }
    }
}

Trade-offs and Best Practices

Using string concatenation with custom delimiters is a legacy pattern often found in older tutorials. While it works for simple cases, it becomes fragile when your data contains the delimiter itself (e.g., if a condition name ever contained an underscore). A more modern approach would be to send JSON or encode the relationship differently, though that adds complexity to the client side.

For immediate fixes within existing codebases, cleaning the value generation to avoid trailing delimiters is the most effective step. This ensures explode returns exactly the number of parts you expect, preventing index-out-of-bounds errors or empty values.

If you are looking for a more robust solution in a Laravel environment, consider using Eloquent relationships or request validation to handle array structures more strictly. The LaravelCompany guide on form requests offers a structured way to validate incoming arrays without relying on fragile string manipulation: https://laravelcompany.com.

By ensuring your input values are clean and your parsing logic handles edge cases like empty split parts, you can reliably process multiple arrays and achieve the desired output format where every selected item carries its full context.

Our network