Support

Account

Home Forums Front-end Issues Ajax submit (checkbox)

Solved

Ajax submit (checkbox)

  • I have an issue, I try to save the front-end form with ajax, my code is working but only with ‘input’ fields and I don´t know how do It with the checkbox fields. Do you know how I can include It?

    Js file:

    function AjaxSubmit() {
    	$('form#modalAjaxTrying :submit').click(function(event){
     		event.preventDefault();
     		var form_data = {'action' : 'acf/validate_save_post'};
     		$('form#modalAjaxTrying :input').each(function(){
     			form_data[$(this).attr('name')] = $(this).val()
     		})
    
     		form_data.action = 'save_my_data';
     		jQuery.post(ajaxurl, form_data)
     		.done(function(save_data){
     			alert('Added successFully :');
     		})
    
    	})
    
    }

    functions.php:

    add_action( 'wp_ajax_save_my_data', 'acf_form_head' );
    add_action( 'wp_ajax_nopriv_save_my_data', 'acf_form_head' );
  • Here the solution. I see the solution in other question from that foro.

    Js file:

    function AjaxSubmit() {
    // 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' );
    				}
    			});
    
    		}
    
    	});
    
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.