Home > Backend Development > PHP Tutorial > How Can I POST Arrays from HTML Forms Without Using JavaScript?

How Can I POST Arrays from HTML Forms Without Using JavaScript?

Mary-Kate Olsen
Release: 2024-11-24 16:35:13
Original
193 people have browsed it

How Can I POST Arrays from HTML Forms Without Using JavaScript?

POSTing Arrays from HTML Forms Without JavaScript

When submitting forms with arrays of data, the standard HTML form submission mechanism typically requires JavaScript for proper handling. However, there is a native HTML approach to POSTing arrays without the need for JavaScript.

To achieve this, use input fields with properly constructed names. For example:

<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">

<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">

<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">

<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">
Copy after login

This approach creates a nested array structure similar to the following in the PHP $_POST[] array:

$_POST[] = array(
    'firstname' => 'value',
    'lastname' => 'value',
    'email' => 'value',
    'address' => 'value',
    'tree' => array(
        'tree1' => array(
            'fruit' => 'value',
            'height' => 'value'
        ),
        'tree2' => array(
            'fruit' => 'value',
            'height' => 'value'
        ),
        'tree3' => array(
            'fruit' => 'value',
            'height' => 'value'
        )
    )
);
Copy after login

This method eliminates the need for JavaScript, improving accessibility for users with JavaScript disabled and simplifying server-side processing.

The above is the detailed content of How Can I POST Arrays from HTML Forms Without Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template