Support

Account

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

  • @ibes Actually, yes! There is a submit action now. Very handy. Also, I included a fix for empty file inputs breaking Safari, the new IE… ;(

    
    acf.addAction('submit', ( $form ) => {
    
      // Fix for Safari Webkit – empty file inputs kill the browser
      // https://stackoverflow.com/a/49827426/586823
      let $fileInputs = $('input[type="file"]:not([disabled])', $form)
      $fileInputs.each(function(i, input) {
        if( input.files.length > 0 ) {
          return;
        }
        $(input).prop('disabled', true);
      })
    
      var formData = new FormData( $form[0] );
    
      // Re-enable empty file $fileInputs
      $fileInputs.prop('disabled', false);
    
      acf.lockForm( $form );
    
      $.ajax({
        url: window.location.href,
        method: 'post',
        data: formData,
        cache: false,
        processData: false,
        contentType: false
      }).done(response => {
        console.log(response);
        acf.unlockForm( $form );
      });
      
    
    });