admin管理员组文章数量:1026989
Despite my best efforts I'm stuck on getting the array keys for form elements submitted to NodeJS and Express and handlebars.
My form elements look like this:
{{#each block}}
<input type='text' name='block_payout[{{id}}]' />
{{/each}
This results in the following markup in the browser:
<input type='text' name='block_payout[14]' />
<input type='text' name='block_payout[15]' />
<input type='text' name='block_payout[16]' />
In PHP this would result in an array as an element of the $_POST array:
$_POST [
block_payout [
14 => value1
15 => value2
16 => value3
]
]
However, the req.body property in Node/Express removes these keys and creates an indexed array:
req.body [
block_payout [
0 => value1
1 => value2
2 => value3
]
]
Since I want to use the key to tie the submitted values to something else this is a big problem for me. Does anyone know how I can get the submitted form data with the correct keys??
Despite my best efforts I'm stuck on getting the array keys for form elements submitted to NodeJS and Express and handlebars.
My form elements look like this:
{{#each block}}
<input type='text' name='block_payout[{{id}}]' />
{{/each}
This results in the following markup in the browser:
<input type='text' name='block_payout[14]' />
<input type='text' name='block_payout[15]' />
<input type='text' name='block_payout[16]' />
In PHP this would result in an array as an element of the $_POST array:
$_POST [
block_payout [
14 => value1
15 => value2
16 => value3
]
]
However, the req.body property in Node/Express removes these keys and creates an indexed array:
req.body [
block_payout [
0 => value1
1 => value2
2 => value3
]
]
Since I want to use the key to tie the submitted values to something else this is a big problem for me. Does anyone know how I can get the submitted form data with the correct keys??
Share Improve this question asked Mar 5, 2014 at 12:11 lewislewis 8589 silver badges15 bronze badges1 Answer
Reset to default 6It seems like the body parser uses an array (which must start at index 0) when it sees only numeric keys, and an object when there is at least one key that is non-numeric. With that in mind, you could try one of the following methods:
Use a hidden form input to force the use of an object over an array. Example:
<input type='hidden' name='block_payout[null]' /> <input type='text' name='block_payout[14]' /> ...
Results in the following body:
{ block_payout: { '14': 'test1', '15': 'test2', '16': 'test3', null: '' } }
Prefix your keys with a non-numeric character to force the object mapping. Example:
<input type='text' name='block_payout[i14]' /> ...
Results in the following body:
{ block_payout: { i14: 'test1', i15: 'test2', i16: 'test3' } }
Despite my best efforts I'm stuck on getting the array keys for form elements submitted to NodeJS and Express and handlebars.
My form elements look like this:
{{#each block}}
<input type='text' name='block_payout[{{id}}]' />
{{/each}
This results in the following markup in the browser:
<input type='text' name='block_payout[14]' />
<input type='text' name='block_payout[15]' />
<input type='text' name='block_payout[16]' />
In PHP this would result in an array as an element of the $_POST array:
$_POST [
block_payout [
14 => value1
15 => value2
16 => value3
]
]
However, the req.body property in Node/Express removes these keys and creates an indexed array:
req.body [
block_payout [
0 => value1
1 => value2
2 => value3
]
]
Since I want to use the key to tie the submitted values to something else this is a big problem for me. Does anyone know how I can get the submitted form data with the correct keys??
Despite my best efforts I'm stuck on getting the array keys for form elements submitted to NodeJS and Express and handlebars.
My form elements look like this:
{{#each block}}
<input type='text' name='block_payout[{{id}}]' />
{{/each}
This results in the following markup in the browser:
<input type='text' name='block_payout[14]' />
<input type='text' name='block_payout[15]' />
<input type='text' name='block_payout[16]' />
In PHP this would result in an array as an element of the $_POST array:
$_POST [
block_payout [
14 => value1
15 => value2
16 => value3
]
]
However, the req.body property in Node/Express removes these keys and creates an indexed array:
req.body [
block_payout [
0 => value1
1 => value2
2 => value3
]
]
Since I want to use the key to tie the submitted values to something else this is a big problem for me. Does anyone know how I can get the submitted form data with the correct keys??
Share Improve this question asked Mar 5, 2014 at 12:11 lewislewis 8589 silver badges15 bronze badges1 Answer
Reset to default 6It seems like the body parser uses an array (which must start at index 0) when it sees only numeric keys, and an object when there is at least one key that is non-numeric. With that in mind, you could try one of the following methods:
Use a hidden form input to force the use of an object over an array. Example:
<input type='hidden' name='block_payout[null]' /> <input type='text' name='block_payout[14]' /> ...
Results in the following body:
{ block_payout: { '14': 'test1', '15': 'test2', '16': 'test3', null: '' } }
Prefix your keys with a non-numeric character to force the object mapping. Example:
<input type='text' name='block_payout[i14]' /> ...
Results in the following body:
{ block_payout: { i14: 'test1', i15: 'test2', i16: 'test3' } }
本文标签: javascriptform element array keys in node and expressStack Overflow
版权声明:本文标题:javascript - form element array keys in node and express - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745662887a2161998.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论