Home > Backend Development > PHP Tutorial > How Can I Submit an Array of Data from an HTML Form Without JavaScript?

How Can I Submit an Array of Data from an HTML Form Without JavaScript?

Linda Hamilton
Release: 2024-11-27 12:02:10
Original
837 people have browsed it

How Can I Submit an Array of Data from an HTML Form Without JavaScript?

Non-JavaScript POST Array Submission from HTML Form

Posting an array of tuples from an HTML form without JavaScript can be a challenge. This question addresses the task of representing a 'User' with an array of 'Trees' within a single form submission.

The provided solution leverages HTML input field naming conventions to organize data into associative arrays within the PHP $_POST superglobal. Here's how it works:

<!-- Example of User Form with array of Trees -->
<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

When the form is submitted, PHP automatically parses the input values into the $_POST array in the following format:

$_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 approach provides a structured way to access both the 'User' and 'Tree' data without relying on JavaScript or complex form processing. It accommodates multiple 'Trees' for a single 'User' while maintaining a clean and organized data representation in PHP.

The above is the detailed content of How Can I Submit an Array of Data from an HTML Form Without 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