2

I am using server side javascript with node.js. Is it possible to make a form input name in html like this:

<input name="name.first">
<input name="name.last">

to get this js object on server side?

req.body = {
    name: {
        first: "firstname",
        last: "lastname"
    }
}

Actually I get not the "name" object with children "first" and "last", but:

'realname.first': 'firstname',
'realname.last': 'lastname',
...

Problem of node.js??

SOLUTION:

Like Eero Otsus said, I had to set brackets, not a period. For example:

<input name="name[first]">

Thanks a lot.

  • Yes it is possible but you need `POST` and `GET` method and a database where to post it. – ZombieChowder Oct 20 '16 at 19:28
  • http://stackoverflow.com/questions/3424860/what-characters-are-allowed-in-the-html-name-attribute-inside-input-tag – Jeff McCloud Oct 20 '16 at 19:28
  • You can't make the browser submit the data in that format (from a form submission at least, you can format it however you like with XMLHttpRequest). It's pretty basic data munging to convert it from one of [the standard data formats](https://www.w3.org/TR/html4/interact/forms.html#h-17.13.4) though. – Quentin Oct 20 '16 at 19:30
  • "Problem of node.js??" — Definitely not, since it doesn't include anything that would parse form data that way. You must be using a library. This is why questions are expected to include a [MCVE]. – Quentin Oct 20 '16 at 19:36
  • You either need something on the client side to format the `POST` data into desired format, or a middleware on the server side to do the same. Although I'm wondering how would `name[first]` and `name[last]` work? PHP will format this into an array, but I doubt node.js will OTB. –  Oct 20 '16 at 19:43
  • node.js will format this too ;) Thanks – Yannik Bürkle Oct 20 '16 at 19:55
  • An actual answer/solution should **not** be edited into your Question. You should leave your question as it was originally, perhaps with clarification as to the *Question*, but not including an Answer within the Question. Create your own Answer with the code you used. [Answering your own question is encouraged](http://stackoverflow.com/help/self-answer), when you have solved the problem yourself. – Makyen Oct 20 '16 at 20:12

1 Answers1

0

I had a similar issue using node and mongoose. My user controller was fighting with my form. I ended up leaving the form < input name="name" > for first and last name inputs. This put them in a "name" array as values 0 and 1. I then changed my controller to name: { first: req.body.name[0], last: req.body.name[1] }. Problem fixed.