|
2 | 2 | * JS to be included on the front end pages with forms |
3 | 3 | */ |
4 | 4 |
|
5 | | -(function($) { |
6 | | - |
7 | | -$(document).ready(function() { |
8 | | - |
9 | | - // ajax form submissions |
10 | | - $('.libre-form').submit(function(e) { |
11 | | - |
12 | | - var $form = $(this); |
13 | | - |
14 | | - // add class to enable css changes to indicate ajax loading |
15 | | - $form.addClass('sending'); |
16 | | - |
17 | | - // reset errors |
18 | | - $form.find('.wplf-error').remove(); |
19 | | - |
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 | | - }); |
| 5 | +(function(){ |
| 6 | +document.addEventListener("DOMContentLoaded", function(){ |
| 7 | + [].forEach.call(document.querySelectorAll(".libre-form"), function(form){ |
| 8 | + |
| 9 | + form.addEventListener("submit", function(e){ |
| 10 | + |
| 11 | + // add class to enable css changes to indicate ajax loading |
| 12 | + form.classList.add("sending"); |
| 13 | + |
| 14 | + [].forEach.call(form.querySelectorAll(".wplf-error"), function(error){ |
| 15 | + // reset errors |
| 16 | + error.parentNode.removeChild(error); |
| 17 | + }); |
| 18 | + |
| 19 | + var data = new FormData(form); |
| 20 | + |
| 21 | + window.data = data; |
| 22 | + |
| 23 | + fetch(ajax_object.ajax_url + '?action=wplf_submit', { |
| 24 | + method: "POST", |
| 25 | + body: data |
| 26 | + }).then(function(response){ |
| 27 | + |
| 28 | + return response.text(); |
| 29 | + |
| 30 | + }).then(function(response){ |
| 31 | + |
| 32 | + response = JSON.parse(response); |
52 | 33 |
|
53 | | - // submit form to ajax handler in admin-ajax.php |
54 | | - /*$.post( ajax_object.ajax_url + '?action=wplf_submit', |
55 | | - $(this).serialize(), |
56 | | - function(response) { |
57 | | - console.log(response); |
58 | 34 | if( 'success' in response ) { |
59 | 35 | // show success message if one exists |
60 | | - $form.after(response.success); |
| 36 | + |
| 37 | + var success = document.createElement("p"); |
| 38 | + success.className = "wplf-success"; |
| 39 | + success.innerHTML = response.success; |
| 40 | + |
| 41 | + form.parentNode.insertBefore(success, form.nextSibling); |
| 42 | + |
61 | 43 | } |
| 44 | + |
62 | 45 | if( 'ok' in response && response.ok ) { |
63 | 46 | // submit succesful! |
64 | | - $form.remove(); |
| 47 | + form.parentNode.removeChild(form); |
65 | 48 | } |
| 49 | + |
66 | 50 | if( 'error' in response ) { |
67 | 51 | // show error message in form |
68 | | - $form.append('<p class="wplf-error error">' + response.error + '</p>'); |
| 52 | + |
| 53 | + var error = document.createElement("p"); |
| 54 | + error.className = "wplf-error error"; |
| 55 | + error.textContent = response.error; |
| 56 | + |
| 57 | + form.appendChild(error); |
69 | 58 | } |
70 | | - } |
71 | | - ).always(function() { |
72 | | - // finished XHR request |
73 | | - $form.removeClass('sending'); |
74 | | - });*/ |
75 | 59 |
|
76 | | - // don't actually submit the form, causing a page reload |
77 | | - e.preventDefault(); |
78 | | - return false; |
| 60 | + form.classList.remove('sending'); |
| 61 | + |
| 62 | + }).catch(function(error){ |
| 63 | + |
| 64 | + console.warn("Fetch error: ", error); |
| 65 | + form.classList.remove("sending"); |
| 66 | + |
| 67 | + }); |
| 68 | + |
| 69 | + // don't actually submit the form, causing a page reload |
| 70 | + |
| 71 | + e.preventDefault(); |
| 72 | + }); |
79 | 73 |
|
80 | 74 | }); |
81 | 75 | }); |
82 | | - |
83 | | -})(jQuery); |
| 76 | +})(); |
0 commit comments