1window.addEventListener('DOMContentLoaded', function () { 2 try { 3 // First, check if ajax_object is available 4 if (typeof ajax_object === 'undefined') { 5 console.error('ajax_object is not defined. Check your PHP enqueue function.'); 6 return; 7 } 8 9 console.log('AJAX Object:', ajax_object); // Debug log 10 11 var form = document.querySelector('.wpcf7-form'); 12 if (form) { 13 console.log('Contact Form 7 found'); // Debug log 14 15 form.addEventListener('submit', function (e) { 16 e.preventDefault(); 17 console.log('Form submission intercepted'); // Debug log 18 19 let formSubmission = new FormData(this); 20 let params = new URLSearchParams(formSubmission); 21 let first = params.get('first_name'); 22 let last = params.get('last_name'); 23 let email = params.get('your-email'); 24 let phone = params.get('phone'); 25 let type = params.get('type'); 26 let company_name = params.get('employer_name'); 27 let message = params.get('message'); 28 29 console.log('Form data:', { first, last, email, phone, type, company_name, message }); // Debug log 30 31 if (first && last && email && phone) { 32 const body = { 33 'firstName': first, 34 'lastName': last, 35 'email': email, 36 'phone': phone, 37 'type': type, 38 'company': company_name, 39 'message': message 40 }; 41 42 console.log('All required fields are filled, submitting...'); // Debug log 43 44 // Show loading state 45 const submitButton = form.querySelector('input[type="submit"]'); 46 const originalText = submitButton ? submitButton.value : ''; 47 if (submitButton) { 48 submitButton.value = 'Submitting...'; 49 submitButton.disabled = true; 50 } 51 52 // Prepare form data for WordPress AJAX 53 const ajaxData = new URLSearchParams({ 54 'action': 'submit_to_n8n', 55 'nonce': ajax_object.nonce, // Include nonce for security 56 'firstName': body.firstName, 57 'lastName': body.lastName, 58 'email': body.email, 59 'phone': body.phone, 60 'type': body.type || '', 61 'company': body.company || '', 62 'message': body.message || '' 63 }); 64 65 console.log('Sending AJAX request to:', ajax_object.ajax_url); // Debug log 66 console.log('AJAX data:', ajaxData.toString()); // Debug log 67 68 // Send data to WordPress AJAX endpoint 69 fetch(ajax_object.ajax_url, { 70 method: 'POST', 71 headers: { 72 'Content-Type': 'application/x-www-form-urlencoded', 73 }, 74 body: ajaxData 75 }) 76 .then(response => { 77 console.log('Response status:', response.status); // Debug log 78 if (!response.ok) { 79 throw new Error(`HTTP error! status: ${response.status}`); 80 } 81 return response.text(); // Get as text first to see raw response 82 }) 83 .then(text => { 84 console.log('Raw response:', text); // Debug log 85 try { 86 const data = JSON.parse(text); 87 console.log('Parsed response:', data); // Debug log 88 89 if (data.success) { 90 console.log('Form submitted successfully:', data.data); 91 // Show success message 92 alert('Thank you! Your form has been submitted successfully.'); 93 form.reset(); 94 } else { 95 console.error('Form submission failed:', data.data); 96 alert('Form submission failed: ' + (data.data || 'Unknown error')); 97 } 98 } catch (parseError) { 99 console.error('Failed to parse JSON response:', parseError); 100 console.error('Raw response was:', text); 101 alert('Server response was not valid JSON. Check console for details.'); 102 } 103 }) 104 .catch(error => { 105 console.error('Network or fetch error:', error); 106 alert('Network error occurred. Please try again.'); 107 }) 108 .finally(() => { 109 // Reset button state 110 if (submitButton) { 111 submitButton.value = originalText; 112 submitButton.disabled = false; 113 } 114 }); 115 } else { 116 console.log('Required fields missing:', { first, last, email, phone }); // Debug log 117 alert('Please fill in all required fields.'); 118 } 119 }); 120 } else { 121 console.error('Contact Form 7 form not found'); 122 } 123 } catch (error) { 124 console.error('Error in form handler:', error); 125 } 126});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.