Home >Backend Development >PHP Tutorial >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]">
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' ) ) );
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!