Support

Account

Home Forums General Issues Frontend form – post via ajax? Reply To: Frontend form – post via ajax?

  • Three years after the original question, now there is a clean way to achieve this:

    
    // call this function on document ready or when your ajax page is loaded
    function renderPage() {
      // initialize the acf script
      acf.do_action('ready', $('body'));
    
      // will be used to check if a form submit is for validation or for saving
      let isValidating = false;
    
      acf.add_action('validation_begin', () => {
        isValidating = true;
      });
    
      acf.add_action('submit', ($form) => {
        isValidating = false;
      });
    
      $('.acf-form').on('submit', (e) => {
        let $form = $(e.target);
        e.preventDefault();
        // if we are not validating, save the form data with our custom code.
        if( !isValidating ) {
          // lock the form
          acf.validation.toggle( $form, 'lock' );
          $.ajax({
            url: window.location.href,
            method: 'post',
            data: $form.serialize(),
            success: () => {
              // unlock the form
              acf.validation.toggle( $form, 'unlock' );
            }
          });
        }
      });
    }
    

    Hope it’s self explanatory.