document.addEventListener('DOMContentLoaded', function () { // Check if the button exists before adding the event listener const importButton = document.getElementById('import-demo-posts'); if (importButton) { importButton.addEventListener('click', function () { const formData = new FormData(); // Append action and nonce to the form data formData.append('action', 'lcp_import_demo_posts'); formData.append('lcp_import_nonce', lcp_ajax_obj.nonce); // Add the nonce passed by wp_localize_script // Send the AJAX request fetch(lcp_ajax_obj.ajax_url, { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { alert(data.data); // Success message } else { alert('Error: ' + (data.data || 'Unknown error')); // Error message } }) .catch(error => { console.error('Error:', error); alert('An error occurred while processing your request.'); }); }); } else { console.warn('Import button not found.'); } });