
document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('myForm').addEventListener('submit', function (e) {
        e.preventDefault(); // Prevent the default form submission

        var isValid = true;
        var errorElements = document.querySelectorAll('.error');
        errorElements.forEach(function (el) {
            el.textContent = ''; // Clear all error messages
        });

        // Validate Company Name
        var companyName = document.getElementById('company_name').value;
        if (companyName.trim() === '') {
            document.getElementById('company_name_Error').textContent = 'Company Name is required.';
            isValid = false;
        }

        // Validate File Upload
        var fileUpload = document.getElementById('client_docs').value;
        if (fileUpload.trim() === '') {
            document.getElementById('client_docs_Error').textContent = 'File upload is required.';
            isValid = false;
        }

        // Validate Text Message
        var textMessage = document.getElementById('text_msg').value;
        if (textMessage.trim() === '') {
            document.getElementById('text_msg_Error').textContent = 'Text Message is required.';
            isValid = false;
        }

        if (isValid) {
            var formData = new FormData(this);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'insert-admin-data.php', true);
            xhr.onload = function () {
                if (xhr.status === 200) {
                    alert('Form submitted successfully.');
                    document.getElementById('myForm').reset(); // Clear the form
                    location.reload();
                } else {
                    alert('An error occurred. Please try again.');
                }
            };
            xhr.onerror = function () {
                alert('An error occurred. Please try again.');
            };
            xhr.send(formData);
        }
    });
});




// $(document).ready(function() {
//     $('#myForm').on('submit', function(event) {
//         event.preventDefault(); // Prevent the default form submission

//         // Gather form data
//         var formData = {
//             name: $('#company_name').val(),
//             email: $('#client_docs').val()
//             email: $('#client_docs').val()
//         };

//         // Send the form data via AJAX
//         $.ajax({
//             type: 'POST',
//             url: 'your-server-endpoint.php', // Replace with your server endpoint
//             data: formData,
//             dataType: 'json',
//             success: function(response) {
//                 console.log('Form submitted successfully:', response);
//                 // Handle the response from the server
//             },
//             error: function(xhr, status, error) {
//                 console.error('Error submitting form:', error);
//                 // Handle any errors
//             }
//         });
//     });
// });
