2

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.

6
  • 1
    not that I know of. you can do input name="attributes[key]" I think but that would be my best guess. You'll want it this way though, attributes[key][] Commented Jul 12, 2014 at 22:08
  • 1
    You are talking about a javascript array.. there's no array on pure HTML. Explain more if I misunderstood. Commented Jul 12, 2014 at 22:10
  • 2
    HTML doesn't have arrays. Commented Jul 12, 2014 at 22:12
  • Okay, I guess this cannot be done without Javascript reformatting it. Commented Jul 12, 2014 at 22:13
  • If you want a data structure like that when the form is submitted, then how you get it depends (mostly) on the language you are using to parse the form data. Are you using JavaScript server side (e.g. with Node)? Commented Jul 12, 2014 at 22:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.