| @@ -1,61 +1,47 @@ | ||
| 1 | -document.addEventListener('DOMContentLoaded', function() { | |
| 2 | - var form = document.getElementById('mxchat-activation-form'); | |
| 3 | - var spinner = document.getElementById('mxchat-activation-spinner'); | |
| 1 | +document.getElementById('mxchat-activation-form').addEventListener('submit', function(e) { | |
| 2 | + e.preventDefault(); // Prevent the form from submitting the normal way | |
| 3 | + | |
| 4 | + var spinner = document.getElementById('mxchat-spinner'); | |
| 4 | 5 | var submitButton = document.getElementById('activate_license_button'); |
| 5 | 6 | var licenseStatus = document.getElementById('mxchat-license-status'); |
| 6 | 7 | |
| 7 | - form.addEventListener('submit', function(e) { | |
| 8 | - e.preventDefault(); | |
| 8 | + // Show the spinner and disable the submit button | |
| 9 | + spinner.style.display = 'inline-block'; | |
| 10 | + submitButton.disabled = true; | |
| 9 | 11 | |
| 10 | - // Show spinner and disable the button to prevent multiple submissions | |
| 11 | - spinner.style.display = 'inline-block'; | |
| 12 | - submitButton.disabled = true; | |
| 12 | + // Gather form data | |
| 13 | + var formData = new FormData(this); | |
| 13 | 14 | |
| 14 | - // Gather form data | |
| 15 | - var formData = new FormData(form); | |
| 16 | - formData.append('action', 'mxchat_activate_license'); | |
| 17 | - formData.append('security', mxchatData.nonce); | |
| 15 | + // Send the AJAX request | |
| 16 | + var xhr = new XMLHttpRequest(); | |
| 17 | + xhr.open('POST', mxchatChat.ajax_url, true); | |
| 18 | + xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); | |
| 19 | + xhr.onload = function() { | |
| 20 | + // Hide the spinner and re-enable the submit button | |
| 21 | + spinner.style.display = 'none'; | |
| 22 | + submitButton.disabled = false; | |
| 18 | 23 | |
| 19 | - // Send the AJAX request | |
| 20 | - var xhr = new XMLHttpRequest(); | |
| 21 | - xhr.open('POST', mxchatData.ajax_url, true); | |
| 22 | - xhr.onload = function() { | |
| 23 | - // Hide the spinner | |
| 24 | - spinner.style.display = 'none'; | |
| 25 | - | |
| 26 | - if (xhr.status >= 200 && xhr.status < 400) { | |
| 27 | - var response = JSON.parse(xhr.responseText); | |
| 28 | - if (response.success) { | |
| 29 | - // Update the license status to active | |
| 30 | - licenseStatus.textContent = 'Active'; | |
| 31 | - licenseStatus.classList.remove('inactive'); | |
| 32 | - licenseStatus.classList.add('active'); | |
| 33 | - | |
| 34 | - // Hide the activation form | |
| 35 | - form.style.display = 'none'; | |
| 36 | - } else { | |
| 37 | - // Re-enable the button | |
| 38 | - submitButton.disabled = false; | |
| 39 | - | |
| 40 | - // Handle the error case | |
| 41 | - alert(response.data || 'There was an error activating the license.'); | |
| 42 | - } | |
| 24 | + if (xhr.status >= 200 && xhr.status < 400) { | |
| 25 | + var response = JSON.parse(xhr.responseText); | |
| 26 | + if (response.success) { | |
| 27 | + // Update the license status to active | |
| 28 | + licenseStatus.textContent = 'Active'; | |
| 29 | + licenseStatus.classList.remove('inactive'); | |
| 30 | + licenseStatus.classList.add('active'); | |
| 43 | 31 | } else { |
| 44 | - // Re-enable the button | |
| 45 | - submitButton.disabled = false; | |
| 46 | - | |
| 47 | - // Handle server error | |
| 48 | - alert('Server error. Please try again.'); | |
| 32 | + // Handle the error case | |
| 33 | + alert(response.data.message || 'There was an error activating the license.'); | |
| 49 | 34 | } |
| 50 | - }; | |
| 35 | + } else { | |
| 36 | + // Handle server error | |
| 37 | + alert('Server error. Please try again.'); | |
| 38 | + } | |
| 39 | + }; | |
| 51 | 40 | |
| 52 | - xhr.onerror = function() { | |
| 53 | - // Hide the spinner and re-enable the button on error | |
| 54 | - spinner.style.display = 'none'; | |
| 55 | - submitButton.disabled = false; | |
| 56 | - alert('Request failed. Please check your internet connection.'); | |
| 57 | - }; | |
| 41 | + xhr.onerror = function() { | |
| 42 | + // Handle connection error | |
| 43 | + alert('Request failed. Please check your internet connection.'); | |
| 44 | + }; | |
| 58 | 45 | |
| 59 | - xhr.send(formData); // Send the form data | |
| 60 | - }); | |
| 46 | + xhr.send(new URLSearchParams(formData).toString()); // Send the form data | |
| 61 | 47 | }); |