POST array from HTML form without javascript

I have a form that is a bit complicated, and I hope to simplify server-side processing (PHP) initially POSTing from an array of tuples.

The first part of the form represents the user:

  • Name
  • Surname
  • Email
  • Address
  • etc.

The second part of the form is the Tree:

  • Fruits
  • Height
  • etc.

The problem is that I need to be able to POST multiple trees for one user in the same form. I would like to send the information as a single user with an array of trees, but this may be too complicated for the form. The only thing that comes to mind is to use javascript to create some JSON message with a User object and an array of Tree objects. But it would be nice to avoid javascript to support more users (some people have scripts disabled).

+60
html post forms
Jan 31 '12 at 2:35
source share
2 answers

check this.

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

it should be the same in the $ _POST [] array (PHP format for easy visualization)

 $_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' ) ) ) 
+111
Jan 31 2018-12-12T00:
source share

You can also place multiple inputs with the same name and store them in an array by adding empty square brackets to the input name as follows:

 <input type="text" name="comment[]" value="comment1"/> <input type="text" name="comment[]" value="comment2"/> <input type="text" name="comment[]" value="comment3"/> <input type="text" name="comment[]" value="comment4"/> 

If you are using php:

 print_r($_POST['comment']) 

You will get the following:

 Array ( [0] => 'comment1' [1] => 'comment2' [2] => 'comment3' [3] => 'comment4' ) 
+20
Jul 21 '17 at 9:08
source share



All Articles