C B
C B

Reputation: 13314

HTML form to XMLHttp request

I am trying to take the following form and convert it to use XMLHttp. Cannot get a working example of how to convert this to use XMLHttp.

<form enctype='multipart/form-data' action='process.do'>
  <input id='myfile' type='file'>
  <input type='hidden' name='systemid' value='a1312423r1rde223e423e'>
  <input type='hidden' name='systemname' value='My value'>
</form>

var x = new XMLHttpRequest()
x.open('POST', 'process.do');
// Where to add systemid, systemname parameters?
x.send($('myfile').files[0])

Upvotes: 0

Views: 47

Answers (1)

Quentin
Quentin

Reputation: 943560

Just send() a FormData object instantiated from the form object.

var x = new XMLHttpRequest()
x.open('POST', 'process.do');
x.send(new FormData(document.querySelector("form")));

That will include all the data from the form.

You'll need to give your file input a name though.

Upvotes: 1

Related Questions