Terinah14
Terinah14

Reputation: 153

Submitting array of textfields using FormData() through ajax

I have an array of input text

<input type="text" name="highlight" value="1"/>
<input type="text" name="highlight" value="2"/>

I used ajax and FormData() to submit it to the server.

var form = $(this);
var formData = new FormData(form[0]);
var target = form.attr('action');

$.ajax({
      url: target,
      type: 'post',
      data: formData,
      processData: false,
      contentType: false,
    })
    .done(function(data){
        console.log(data.message);
    });

**Expected Server result:**highlight=['1','2']
**Actual Server result:**highlight=2
Im using nodejs as server

Upvotes: 0

Views: 61

Answers (1)

MrMadsen
MrMadsen

Reputation: 3012

You need to add brackets to your input name attributes highlight[]. This way the browser knows they are part of the same array and won't overwrite one with the other.

<input type="text" name="highlight[]" value="1"/>
<input type="text" name="highlight[]" value="2"/>

Upvotes: 1

Related Questions