send form data to server post javascript

Solutions on MaxInterview for send form data to server post javascript by the best coders in the world

showing results for - "send form data to server post javascript"
Blanche
27 Jul 2017
1// Because we want to access DOM nodes,
2// we initialize our script at page load.
3window.addEventListener( 'load', function () {
4
5  // These variables are used to store the form data
6  const text = document.getElementById( "theText" );
7  const file = {
8        dom    : document.getElementById( "theFile" ),
9        binary : null
10      };
11
12  // Use the FileReader API to access file content
13  const reader = new FileReader();
14
15  // Because FileReader is asynchronous, store its
16  // result when it finishes to read the file
17  reader.addEventListener( "load", function () {
18    file.binary = reader.result;
19  } );
20
21  // At page load, if a file is already selected, read it.
22  if( file.dom.files[0] ) {
23    reader.readAsBinaryString( file.dom.files[0] );
24  }
25
26  // If not, read the file once the user selects it.
27  file.dom.addEventListener( "change", function () {
28    if( reader.readyState === FileReader.LOADING ) {
29      reader.abort();
30    }
31
32    reader.readAsBinaryString( file.dom.files[0] );
33  } );
34
35  // sendData is our main function
36  function sendData() {
37    // If there is a selected file, wait it is read
38    // If there is not, delay the execution of the function
39    if( !file.binary && file.dom.files.length > 0 ) {
40      setTimeout( sendData, 10 );
41      return;
42    }
43
44    // To construct our multipart form data request,
45    // We need an XMLHttpRequest instance
46    const XHR = new XMLHttpRequest();
47
48    // We need a separator to define each part of the request
49    const boundary = "blob";
50
51    // Store our body request in a string.
52    let data = "";
53
54    // So, if the user has selected a file
55    if ( file.dom.files[0] ) {
56      // Start a new part in our body's request
57      data += "--" + boundary + "\r\n";
58
59      // Describe it as form data
60      data += 'content-disposition: form-data; '
61      // Define the name of the form data
62            + 'name="'         + file.dom.name          + '"; '
63      // Provide the real name of the file
64            + 'filename="'     + file.dom.files[0].name + '"\r\n';
65      // And the MIME type of the file
66      data += 'Content-Type: ' + file.dom.files[0].type + '\r\n';
67
68      // There's a blank line between the metadata and the data
69      data += '\r\n';
70
71      // Append the binary data to our body's request
72      data += file.binary + '\r\n';
73    }
74
75    // Text data is simpler
76    // Start a new part in our body's request
77    data += "--" + boundary + "\r\n";
78
79    // Say it's form data, and name it
80    data += 'content-disposition: form-data; name="' + text.name + '"\r\n';
81    // There's a blank line between the metadata and the data
82    data += '\r\n';
83
84    // Append the text data to our body's request
85    data += text.value + "\r\n";
86
87    // Once we are done, "close" the body's request
88    data += "--" + boundary + "--";
89
90    // Define what happens on successful data submission
91    XHR.addEventListener( 'load', function( event ) {
92      alert( 'Yeah! Data sent and response loaded.' );
93    } );
94
95    // Define what happens in case of error
96    XHR.addEventListener( 'error', function( event ) {
97      alert( 'Oops! Something went wrong.' );
98    } );
99
100    // Set up our request
101    XHR.open( 'POST', 'https://example.com/cors.php' );
102
103    // Add the required HTTP header to handle a multipart form data POST request
104    XHR.setRequestHeader( 'Content-Type','multipart/form-data; boundary=' + boundary );
105
106    // And finally, send our data.
107    XHR.send( data );
108  }
109
110  // Access our form...
111  const form = document.getElementById( "theForm" );
112
113  // ...to take over the submit event
114  form.addEventListener( 'submit', function ( event ) {
115    event.preventDefault();
116    sendData();
117  } );
118} );
queries leading to this page
where to send post data from formjs xhr send formdata postform send data to javascriptjs submit form data postjs post form datapost form data in javascriptsend post request with form data in javascriptjavascript sending form data as posthow to send form data using javascriptsend form data to server javascripthow does a form send data in javascriptpure javascript send post data with html formjavascript form data parameters postjs send form data postjavascript form data postsend post data in javascript html formadding form data from a post request to a javascript objecthow to make a post request to a server in javascript with a formhow to get form data on post javascriptsend form data in post request javascriptform data post in jsget data from form in html and send to java scripthow to send form data to server using javascriptpure javascript send post data with a formjavascript send form data and get responsesend form data in post requestjavascript post request send html formjs post form data to urljavascript post form dataform data post request code jshow to send form data to api using javascriptjs make post request with form datajavascript post data from form on submitform data post request javascriptform data post request javascriptjavascript get post data from formpost form data with js functionjavascript send a post request of a formsend form data to script htmljs http send form datasend data in form data javascriptpost form data to a serverjs send form post and responsehow to send form data to api post via java scriptjavascript send post request form data filesend data from form to javascripthow to send form data in http requesthow to dsend form data in post request in jshtml post form send datahow to send post with form datasend post request from html form jsjavascript send form datajavascript send data as formsend data from form javascriptjavascript send data from formhow to send post with form data 28 29pass post data from formadd form data to http post jsjavascript get form post datasend form data to server post javascripthow to send form data in post request in jshow to post form data in javascriptpost form data in jspost form data to api javascriptjs send post form datapost form data javascripthow to form send data from client to server in javascriptsend post with form and datajs send post formdata with jsonget data from form and send to java scriptform data send javascripthow to send form data in javascriptjavascript post form data backinto codesend post data javascript without formjs form data in postpost data use form jspost form data using javascriptjavascript send in post formform data javascript posthow to send data with form using javascripthtml form post to javascript function with dataexample code to send form data in html javascriptreceive data when using form to post javascriptpost form data usinng javascriptpost form data to database jssend form post javascript extra datajavascript get data from submitted form and postpost form data to api using javascriptsend form data using javascriptjavascript how to send form data to websitehow to use html form to send data post nod jssend post request from form via javascri 5bptsend form data with post request html formjavascript post form data to urlsend post request with form data javascriptjavascript form data post requestsend data to api using form data javascriptsend form data to server post javascript