Support

Account

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

  • Hi @rasso & @danielrutkowski,

    I have used your code to do some tests and I finally modify it to limit the number of Ajax call :

    
    	acf.do_action('ready', $('body'));
    
        $('.acf-form').on('submit', function(e){
            e.preventDefault();
        });
    
    	acf.add_action('submit', function($form){
    		$.ajax({
    			url: window.location.href,
    			method: 'post',
    			data: $form.serialize(),
    			success: function(data) {
    				acf.validation.toggle($form, 'unlock');
                    alert(data);
    			}
    		});
    	});
    

    In the actual code the AJAX success has no response data. If you want to get the “update message” set in the form declaration you can use this hook :

    
    add_action('acf/submit_form', 'my_acf_submit_form', 10, 2);
    function my_acf_submit_form( $form, $post_id ) {
    
        if(is_admin())
            return;
    
        //Test if it's a AJAX request.
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
            echo $form['updated_message'];
            die();
        }
    
    }

    I hope this code can help you! 😉