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]">
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' ) ) )
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!