Explain Codes LogoExplain Codes Logo

Multiple inputs with same name through POST in PHP

php
form-data
data-sanitization
best-practices
Nikita BarsukovbyNikita Barsukov·Oct 3, 2024
TLDR

HTML inputs sharing the same name[] attribute become an array in PHP. Read these values with $_POST['name'] and iterate through the array for each individual value.

HTML illustration:

<input type="text" name="inputs[]"> <input type="text" name="inputs[]"> <!-- And so on -->

PHP illustration:

foreach ($_POST['inputs'] as $input) { // Always clean your inputs, cleaner is better! echo htmlspecialchars($input) . '<br>'; }

Notes to self:

  • Employ name[] for PHP to group inputs as an array.
  • Use $_POST['name'] for access, run through results with a loop.

Efficient management of form data

Managing multiple name[] attributes in a form is a cakewalk in PHP, boosting efficiency. Groups of related inputs can be aptly managed using indexes or nested arrays in the name[] attribute, allowing you to handle multi-dimensional arrays. In turn, it offers an advanced structure suitable for complex forms, like so:

<input type="text" name="shipping[address]"> <input type="text" name="shipping[city]"> <input type="text" name="billing[address]"> <input type="text" name="billing[city]">

Resultant $_POST superglobal array in PHP:

$_POST['shipping'] = ['address' => '123 St.', 'city' => 'New York']; $_POST['billing'] = ['address' => '456 Ln.', 'city' => 'Los Angeles'];

Please, for the love of '*' (read as asterisk, not star), sanitize every single piece of data that finds its way through to prevent nasty things like XSS attacks.

Ordering inputs, optimizing processing

Structuring data

Another day, another array. That's right! Ensure the structure of these multi-input arrays fit your application's requirements to prevent potential errors when processing form data.

The loop is your friend

foreach loops, or their variations in PHP, are your superheroes in disguise for handling each piece of the input data with accuracy. Irrespective of the action - storing in a database, shooting an email or anything else, loop through the inputs methodically with relevant operations enclosed.

Data - handle with care

When working with forms, it's easy to make a faux pas and forget to include an index on an array element. Always cross-check your form's name attributes to prevent unintended overwrites and ensure data integrity.

Code cleanliness is next to... you know what

Maintain the organizability of your form code by logically naming your inputs. Not only will it help when reviewing code, but also when debugging or expanding your application.

Unlimited potential

Take advantage of the boundless nature of PHP arrays to handle any number of inputs. Each input adorned with the name[] structure grants PHP the flexibility to handle data-rich forms.

Safeguarding your application

Always practice data sanitization and validation for your inputs. The little effort put in here will help keep your application secure. To ensure this, engage the filter_input() function and its companions in PHP to cleanse the data before use.