Support

Account

Home Forums Front-end Issues Clear/reset form after submit

Solving

Clear/reset form after submit

  • First, this plugin is amazing! I can’t believe what it can do. Now, I must be missing something really obvious but for the life of me I can’t figure this out.

    My site is setup so that a guest user comes fills out a form and submits. This form creates a new post (for admin purposes). The values from the form are passed to a table (not handled by ACF). Using an additional ACF field group, each post gets an “admin” section with appropriate fields. In the table each row has an “edit” link that takes an admin to the WP backend to edit that particular post. This all works perfectly.

    Here’s my question. I’d like to give the admin a mini-CMS on the table page so that instead of going to the backend of WP, when they click the link, the fields from the admin side are displayed with the appropriate content for each post. If no post is selected/clicked on, I’d like the fields to be blank/default values (naturally). However they’re not clearing after submission/redirection.

    Here’s the code I’m trying.

    
    //add a shortcode to let admins work on requests from the front-end instead of needing to go back to WP.
    function output_admin_acf_form() {
    	$post_id = $_GET['post_id'];
    
    	if (!post_id || !log_in_check()) {
    		$content = null;
    	} else {
    		$options = array(
    			'post_id' => $post_id,
    			'field_groups' => array('group_5888d67caf51a'),
    			'return' => home_url('/request-log'),
    			'submit_value' => 'Update Request',
    			'updated_message' => __('Post updated', 'acf')
    		);
    		$content = acf_form($options);
    	}
    	return $content;
    }
    
    // add the shortcode
    function acf_admin_shortcode($atts, $content = null) {
    	$content = output_admin_acf_form();
    	return $content;
    }
    add_shortcode('admin_form', 'acf_admin_shortcode');
    
    // update the post
    function pre_save_admin_post($post_id) {
    	if (is_admin()) {
    		return;
    	}
    	$post = array(
    		'ID' => $post_id,
    		'post_status' => 'publish'
    	);
    	wp_update_post($post);
    	return $post_id;
    }
    add_filter('acf/pre_save_post', 'pre_save_admin_post');
    
    // try to reset the form.
    function reset_form_on_submit() {
    	?>
    		<script>
    		var submitDiv = document.querySelector('.acf-form-submit');
    		var submitButton = submitDiv.firstElementChild;
    		var resetHandler = function(evt) {
    			evt.preventDefault();
    			// post (for some reason) is the ID of the form.
    			var post = document.getElementById('post');
    			post.reset();
    		}
    		submitButton.addEventListener('click', resetHandler());
    
    		</script>
    	<?php
    }
    add_action('admin_post_reset_form', 'reset_form_on_submit');
    
  • To be honest, I haven’t got a clue, so this is just a guess. You could try removing the $post_id query argument in your pre_save_post filter.

    I don’t think that resetting the form before it is submitted is a good idea, which is what I’m assuming that the JS code above is doing. Wouldn’t that cause default values to be sent in the submission of the form? More than likely that code is never happening or you’d see other problems. You might be able to reset the form on the acf/ready JS action https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/, but I’m not sure.

  • @hube2 Thanks for the link. I’ll try that out and see how it goes. I’m sure I’m missing something really obvious here.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Clear/reset form after submit’ is closed to new replies.