POST an array from an HTML form without javascript

POST an array from an HTML form without javascript

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

Stefan Bogdanescu

Founder & Senior Architect

POST an array from an HTML form without javascript

Handling complex forms often requires transmitting nested data structures to the server. A common scenario involves a single entity, like a User, paired with multiple related entities, such as a list of Trees. While modern development frequently relies on JavaScript to serialize these relationships into JSON before submission, there are valid reasons to avoid client-side scripting entirely. Users may have scripts disabled for privacy or performance reasons, and relying on it can break the application for that specific segment of your audience. Fortunately, you can achieve this goal using standard HTML form techniques without writing a single line of JavaScript.

The core mechanism for sending arrays from an HTML form lies in how input names are constructed. When a user submits a standard form, the browser automatically serializes the inputs into a URL-encoded query string (e.g., name=John&age=25). To send multiple items of the same type—like several trees for one user—you must name your inputs using an indexed array syntax.

Consider a simple example where you want to collect three trees. You would define multiple input fields with incrementing numeric indices in their name attributes:

<form method="POST" action="/process">
    <!-- User Data -->
    <input type="text" name="user[first_name]" value="Alice">
    <input type="text" name="user[last_name]" value="Smith">
    
    <!-- First Tree -->
    <input type="text" name="tree[0][fruit]" value="Apple">
    <input type="number" name="tree[0][height]" value="10">
    
    <!-- Second Tree -->
    <input type="text" name="tree[1][fruit]" value="Pear">
    <input type="number" name="tree[1][height]" value="12">
    
    <!-- Third Tree -->
    <input type="text" name="tree[2][fruit]" value="Plum">
    <input type="number" name="tree[2][height]" value="8">

    <button type="submit">Save</button>
</form>

When this form is submitted, the browser sends the following data in the request body:

user[first_name]=Alice&user[last_name]=Smith&tree[0][fruit]=Apple&tree[0][height]=10&tree[1][fruit]=Pear...

On the server side, frameworks like PHP handle this parsing automatically. The $_POST superglobal will not be a flat list of strings; instead, it will reconstruct the nested structure into associative arrays. In PHP, accessing $user['first_name'] is straightforward, but retrieving the trees requires iterating over the array key for each tree, which PHP populates automatically based on the numeric indices provided in the form names.

This approach works seamlessly with Laravel's request handling as well. If you are using a framework, you might prefer to validate and access this data within a single model or controller logic rather than splitting it across multiple files. For instance, you can define an accessor or mutator on your User model that accepts the $trees array and saves each tree record individually via a polymorphic relationship or a separate table with a foreign key to the user.

There are specific trade-offs to consider when implementing this pattern. The primary limitation is scalability regarding field names. If you expect a very large number of trees, hard-coding numeric indices in HTML can become unwieldy and prone to errors if fields are added or reordered. Additionally, because the browser relies on the order of appearance for non-indexed arrays (though less critical here due to explicit indexing), it is best practice to always use explicit integer indices (0, 1, 2) rather than relying on implicit ordering, which can sometimes behave unexpectedly across different browsers or older HTTP clients.

Another consideration is validation. Since the data arrives as a nested array structure, your server-side validation logic must account for this shape. In Laravel, you would define rules like ['tree.*.fruit' => 'required|string'] to handle the variable depth of the collection without needing to iterate manually first. This keeps your controller code clean and leverages the framework's built-in capabilities.

By utilizing indexed input names, you maintain full compatibility with users who disable JavaScript while still supporting complex data structures. This method ensures that your application remains robust regardless of the client environment, adhering to the principle of graceful degradation where possible.

Our network