Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Form: POST an array of objects

Tags:

Submitting a class roster. Adding 3 students at once. Each student has first, last, age.

Question: How can we get all of the students in an array of arrays?

students[0] => Array (   ["first"] => "first name for 0",   ["last"] => "last name for 0",   ["age"] => "age for 0" ), students[1] => Array (   ["first"] => "first name for 1",   ["last"] => "last name for 1",   ["age"] => "age for 1" ),  ...   

Details
For one student:

<input type="text" name="first">   <input type="text" name="last">   <input type="text" name="age">   

We can return multiple students in separate arrays like this:

<input type="text" name="students[first][]">   <input type="text" name="students[last][]">   <input type="text" name="students[age][]">   

which returns an array of firsts, lasts and ages

students["first"] = [array of first names] students["last"] = [array of last names] students["age"] = [array of ages]   

Theoretically we can get all the info for a student by accessing the same index (say "3" for each array).

We do not want to programatically add an index in the form.
Do not want:

<input type="text" name="students[hardcoded_index][first]">   <input type="text" name="students[hardcoded_index][last]">   <input type="text" name="students[hardcoded_index][age]">   

If for any reason it matters, we are using Rails for views but can use form helpers or HTML.

like image 939
csi Avatar asked Dec 16 '15 13:12

csi


People also ask

How can we send arrays in HTML forms?

You can create arrays in form fields using square brackets. That means you can name fields like account[first_name] and account[last_name] and in most server-side languages, you will end up with an 'account' array.

How do you post a form in HTML?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

How do you add an element to an array of objects?

The push() method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method. The object is hence added to the end of the array.


1 Answers

tl;dr: Add empty brackets ([]) after students to the input names.

Fiddling with Rack::Utils.parse_nested_query it seems you can get the payload you want like this:

<!-- first student --> <input type="text" name="students[][first]"> <input type="text" name="students[][last]"> <input type="text" name="students[][age]">  <!-- second student --> <input type="text" name="students[][first]"> <input type="text" name="students[][last]"> <input type="text" name="students[][age]"> 

Note the empty brackets ([]) after students. This tells Rack you want the students param to be an array. Subsequent params encountered (with the same name) will start a new element.

POST /myroute?students[][first]=foo&students[][last]=bar&students[][age]=21&students[][first]=baz&students[][last]=qux&students[][age]=19

Gets parsed like this:

{"students" => [   {     "first" => "foo",      "last" => "bar",       "age" => "21"   },   {     "first" => "baz",      "last" => "qux",       "age" => "19"   } ]} 

Further reading: http://codefol.io/posts/How-Does-Rack-Parse-Query-Params-With-parse-nested-query

like image 152
messanjah Avatar answered Oct 05 '22 23:10

messanjah