Home > Article > Backend Development > How Can I Submit an Array from an HTML Form Without JavaScript?
POST an Array from an HTML Form Without JavaScript
This question proposes a challenge of sending an array of tuples from a complex HTML form to simplify server-side processing. The form includes two sections: one for user information and another for a list of trees. The goal is to avoid using JavaScript and support a wider user base.
The solution presented in the answers involves structuring the form inputs as follows:
<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]">
This structure results in the following $_POST array in PHP 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' ) ) )
By following this approach, the server can access the user and tree data as an array of arrays, facilitating efficient processing without the need for JavaScript.
The above is the detailed content of How Can I Submit an Array from an HTML Form Without JavaScript?. For more information, please follow other related articles on the PHP Chinese website!