77$ ( document ) . ready ( function ( ) {
88
99 // ajax form submissions
10- $ ( '.libre-form' ) . submit ( function ( e ) {
10+ $ ( '.libre-form' ) . submit ( function ( e ) {
1111
1212 var $form = $ ( this ) ;
1313
14- // add class to enable css changes to indicate ajax loading
14+ // add class to enable css changes to indicate ajax loading
1515 $form . addClass ( 'sending' ) ;
1616
1717 // reset errors
1818 $form . find ( '.wplf-error' ) . remove ( ) ;
1919
20+ var data = new FormData ( $form [ 0 ] ) ; // includes files too, but for some reason we don't see them at the server
21+
22+
23+ window . data = data ;
24+
25+
26+ fetch ( ajax_object . ajax_url + '?action=wplf_submit' , {
27+ method : "POST" ,
28+ body : data
29+ } ) . then ( function ( response ) {
30+ //console.log(response);
31+ return response . text ( ) ;
32+ } ) . then ( function ( response ) {
33+ console . log ( response ) ;
34+ response = JSON . parse ( response ) ;
35+ if ( 'success' in response ) {
36+ // show success message if one exists
37+ $form . after ( response . success ) ;
38+ }
39+ if ( 'ok' in response && response . ok ) {
40+ // submit succesful!
41+ $form . remove ( ) ;
42+ }
43+ if ( 'error' in response ) {
44+ // show error message in form
45+ $form . append ( '<p class="wplf-error error">' + response . error + '</p>' ) ;
46+ }
47+ $form . removeClass ( 'sending' ) ;
48+ } ) . catch ( function ( error ) {
49+ console . log ( "Fetch error: " , error ) ;
50+ $form . removeClass ( 'sending' ) ;
51+ } ) ;
52+
2053 // submit form to ajax handler in admin-ajax.php
21- $ . post ( ajax_object . ajax_url + '?action=wplf_submit' ,
22- $ ( this ) . serialize ( ) ,
54+ /* $.post( ajax_object.ajax_url + '?action=wplf_submit',
55+ $(this).serialize(),
2356 function(response) {
2457 console.log(response);
2558 if( 'success' in response ) {
2659 // show success message if one exists
2760 $form.after(response.success);
28- }
61+ }
2962 if( 'ok' in response && response.ok ) {
3063 // submit succesful!
3164 $form.remove();
@@ -38,7 +71,7 @@ $(document).ready(function() {
3871 ).always(function() {
3972 // finished XHR request
4073 $form.removeClass('sending');
41- } ) ; ;
74+ });*/
4275
4376 // don't actually submit the form, causing a page reload
4477 e . preventDefault ( ) ;
@@ -48,4 +81,3 @@ $(document).ready(function() {
4881} ) ;
4982
5083} ) ( jQuery ) ;
51-
0 commit comments