• Resolved Nathan Adams

    (@nathan-adams)


    For a submit button that submits via AJAX (by having the ‘Redirect After Submit’ option set to ‘None’), is there a way of tying an action to it via jQuery?

    I’ve set up forms that by default have the submit button disabled, via a CSS class on the parent form – “unpublished”, that is remove if any value is changed in the form. When the form is submitted, I’d like to add that “unpublished” class back to the form.

    I’ve tried both:

    $('.frontend-form').on('submit', function(e) {
    $(this).removeClass('unpublished');
    });

    and

    $(document.body).on('click', '.frontend-form .fea-submit-button', function(e) {
    $(this).closest('form').removeClass('unpublished');
    });

    but neither seem to fire when submitting.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Nathan Adams

    (@nathan-adams)

    Additionally, is there a way to only have the plugin load for logged in users? I only have one form, which I’ve limited the visibility to logged in users only, but it still loads a bunch of assets in wp_footer (frontend-admin-min.css, dashicons.min.css, acf.global.min.css, acf-input.min.css) even when not logged in, which is unnecessary.

    Thread Starter Nathan Adams

    (@nathan-adams)

    I’ve managed to work out solutions to both.

    For the action on submit, I used MutationObserver to pick up on the addition of the success message.

    document.addEventListener('DOMContentLoaded', function () {
    var forms = document.querySelectorAll('.frontend-form');

    for (var i = 0; i < forms.length; i++) {
    (function(form) {
    var observer = new MutationObserver(function(mutationsList) {
    for (var j = 0; j < mutationsList.length; j++) {
    var mutation = mutationsList[j];

    if (mutation.addedNodes.length > 0) {
    if (form.querySelector('.acf-notice.-success')) {
    console.log('Success notice detected. Removing .unpublished class.');
    form.classList.remove('unpublished');
    observer.disconnect();
    break;
    }
    }
    }
    });

    observer.observe(form, { childList: true, subtree: true });
    })(forms[i]);
    }
    });

    and to remove the files loaded by the plugin when not required I added an action to functions.php

    add_action('wp_enqueue_scripts', 'remove_frontendadmin_styles', 100);

    function remove_frontendadmin_styles() {
    if (!is_user_logged_in() && !is_admin()) {
    wp_dequeue_style('dashicons');
    wp_dequeue_style('acf-global-css');
    wp_dequeue_style('acf-input-css');
    wp_dequeue_style('fea-public-css');

    wp_deregister_style('dashicons');
    wp_deregister_style('acf-global-css');
    wp_deregister_style('acf-input-css');
    wp_deregister_style('fea-public-css');
    }
    }

    Happy to hear of more elegant solutions if they exist though!

    Plugin Author Shabti Kaplan

    (@shabti)

    Hello @nathan-adams

    I apologize for the late response. WE have been out of commision to due to holidays + strep throat, but we are now back online.

    Here is an example code snippet that uses our js hook “frontend_form_success. I am actually using this in a project to update a live preview when the form is submitted succesfully. You can use console.log to inspect the parameters “data” and “form”


    acf.addAction( 'frontend_form_success', function( data, form ) {
    if( data.preview ){
    //replace '.live-preview' with the preview
    var preview = document.querySelectorAll('.live-preview');

    preview.forEach(function(preview){
    preview.innerHTML = data.preview;
    });

    //form is a jQuery object, so we need to get the first element to get the DOM element
    form = form[0];
    form.querySelector('input[name="_acf_status"]').value = '';

    //
    }
    });
    Plugin Author Shabti Kaplan

    (@shabti)

    Just to clarify, data.preview is null by default, but you can use the php hook frontend_admin/form/submission_preview to generate one, like this:

    add_filter( 'frontend_admin/form/submission_preview', 'form_preview', 10, 2 );

    function form_preview( $preview, $form ) {
    return '<h3>Preview Mode</h3>';
    }
    Thread Starter Nathan Adams

    (@nathan-adams)

    Thanks @shabti hope you’re feeling better now.

    That’s much cleaner! I was able to adapt that to just this to remove the unpublished class. Working exactly as needed.

    acf.addAction( 'frontend_form_success', function( data, form ) {
    form.removeClass('unpublished');
    });
    Plugin Author Shabti Kaplan

    (@shabti)

    Awesome. I am glad you are enjoying the plugin (;

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Firing a jQuery command when form is submitted’ is closed to new replies.