I have a form which uses a get method. There is a select element in the form that allows the user to select multiple options. This gets passed to the URL when the user clicks submit. However, it does not get neatly presented in the URL at all. Is there a a way to pass the array in a neat way to the URL? (I’m using .pug because I’m using express.)
form(action='/FormHandler' method='get') label(for='tags') TAGS select(name='tags[]' multiple='') option(value="tag1") tag1 option(value="tag2") tag2 option(value="tag3") tag3 input(type='submit' value='submit')
I then want to take apart the URL using the url.parse(request.url, true).query;
method in express in NodeJS.
Advertisement
Answer
The request.query
property of express gives you the desired array representation in Javascript. If tags 1 and 2 are selected, the resulting request
http://server/formHandler?tags[]=tag1&tags[]=tag2
leads to
request.query = {"tags":["tag1","tag2"]}
There is then probably no need to parse the URL yourself using url.parse
.