Array in post converts to string

Viewed 137

in html if you do

<form action="" method="post">
  <select multiple name="items[]">
    <option value.... 

the post result will be an array. With vue, if you do

<input type="hidden" name="items[]" v-model="myArray">

you get back an array with one element containing a string "1,2,3". Essentially, v-model converts into a string on the post request. How can I get it to post an actual array?

1 Answers

This isn't the result of anything v-model is doing. Even in vanilla JavaScript/HTML, if you set an array value to an input and post it, you will get a string.

In order to post an array (without ajax) from <input>, you need multiple <input> with the same name:

<input
  type="hidden"
  name="items[]"
  v-for="(item, index) in myArray"
  :key="index"
  :value="item"
/>

No need for v-model either since the input is hidden.

Related