jQuery(document).ready(function($) {
    $('#myForm').on('submit', function(e) {
        e.preventDefault();

        $('#result').html('<p>Sending...</p>');

        $.ajax({
            url: myAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'my_ajax_submit',    // This triggers the PHP function
                nonce:  myAjax.nonce,
                name:   $('input[name="name"]').val(),
                email:  $('input[name="email"]').val()
            },
            success: function(response) {
                if (response.success) {
                    $('#result').html('<p style="color:green;">' + response.data.message + '</p>');
                    $('#myForm')[0].reset(); // Clear form
                } else {
                    $('#result').html('<p style="color:red;">' + response.data.message + '</p>');
                }
            },
            error: function() {
                $('#result').html('<p style="color:red;">Server error!</p>');
            }
        });
    });
});