Kousha
Kousha

Reputation: 36189

HTML Form as Array with key - value pair

Is it possible to create a form in pure HTML so that when it is passed, the result is the array, but the 'name' becomes the index 'key' and the value becomes the index 'value'?

For instance, say my form is to have multiple attributes. First attribute is a color, second is a abbv, and third is a cost.

So I'd like to get an array of the following form:

attributes: [
    {
        key : 'color',
        value: 'the color the user picks'
    },
    {
        key : 'abbv',
        value: 'the abbreviation the user types'
    },
    {
        key : 'cost',
        value: 'the cost the user assigns'
    }    
]

I want to know if this is possible in pure HTML. I know I can perform this in Javascript.

Upvotes: 2

Views: 2146

Answers (1)

Noah Freitas
Noah Freitas

Reputation: 17430

What you are asking for is an application/json encoding type for HTML form submissions.

This type is not currently specified for HTML, which, as of version 5, specifies three encoding types:

  1. application/x-www-form-urlencoded
  2. multipart/form-data
  3. text/plain

And of course, if it were supported, there is no guarantee that it would serialize the data in the format you've specified.

Upvotes: 1

Related Questions