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

        var formData = $(this).serialize(); // Serialize form data
        var formAction = $(this).attr('action'); // Get form action URL

        // AJAX call to submit form data
        $.ajax({
            url: formAction,
            method: 'POST',
            data: formData,
            dataType: 'json', // Ensure the response is parsed as JSON
            success: function(response) {
                console.log(response);
                if (!response.error) {
                    $('#msgSubmit').text('Added successfully!').removeClass('hidden').addClass('alert alert-success');
                } else {
                    $('#msgSubmit').text(response.message).removeClass('hidden').addClass('alert alert-danger');
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $('#msgSubmit').text('Request failed. Please try again.').removeClass('hidden').addClass('alert alert-danger');
            }
        });
    });
});