Home >Backend Development >PHP Tutorial >Can I POST an Array from an HTML Form without JavaScript?

Can I POST an Array from an HTML Form without JavaScript?

Linda Hamilton
Linda HamiltonOriginal
2024-12-15 05:45:12813browse

Can I POST an Array from an HTML Form without JavaScript?

POST an Array from an HTML Form without JavaScript

Question:

Can an array of tuples be natively POSTed from an HTML form without using JavaScript? The form consists of two parts: "User" and "Tree," where multiple trees can be associated with each user.

Answer:

Yes, it is possible to POST an array from an HTML form without using JavaScript. Here's how:

HTML Markup:

<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]">

PHP Processing:

In PHP, the POSTed data will be available in the $_POST array as follows:

$_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'
        )
    )
)

This structure allows you to easily access the User information and the array of Trees associated with it.

The above is the detailed content of Can I POST an Array from an HTML Form without JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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