• Resolved Ian Chapman

    (@ianchapman)


    I am trying to link a multi-step form together using the query_vars (as was explained in a separate thread). I am using your previously posted logic about how to include multiple forms on a single page. See the code below.

    
    $formStep1Name = 'shopping-setup-competitors';
    $formStep2Name = 'shopping-setup-services';
    
    // Check if a portion of the form has already been submitted
    if (acf_maybe_get_POST('_acf_form')) {
        // Get submitted form data
        $form = json_decode(acf_decrypt($_POST['_acf_form']), true);
    
        // If Form Step 1 has been submitted
        if (acf_maybe_get($form, 'form_name') === $formStep1Name) {
            // Display the Step 2 form
            acfe_form($formStep2Name);
        } elseif(acf_maybe_get($form, 'form_name') === $formStep2Name) {
        	// User has finished filling out the form
            echo 'Success!';
        }
    // No Form submitted
    } else {
        // No form has been submitted, so we display the Step 1 form
        acfe_form($formStep1Name);
    }
    

    In Step 1, I am creating a new custom post. Step 2 then displays another set of fields associated with the custom post type.

    The pages connect, and the query variable is being passed. I know this because I am able to output the original post ID on Step 2 and the IDs match up. And the second part of the form is displayed correctly.

    However the second step still doesn’t save anything to the database.

    Here are screenshots of my Step 1 setup …
    Actions Tab | Save Tab | Submission Tab

    Here are screenshots of my Step 2 setup …
    Actions Tab | Save Tab | Load Tab | Submission Tab

    Let me know if you spot anything that would explain why the second step is not saving the data.

    Thanks,
    Ian

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello Ian,

    Thanks for the detailed report. I ran some tests, and you are right, the Step 2 Form doesn’t retrieves the Query Var. Now I’m thinking about it, it’s perfectly normal because Query Vars are just variables. They aren’t submitted by the form.

    In your case, the Query Var exists when displaying the Step 2 Form. But at the submission, the actions are processed in the next page load, and the Query Var isn’t there anymore.

    I’ve made a little graph to explain it, hope it’s more clear:

    
    +--------------------------------------------+
    | Headers: Empty                             |
    +--------------------------------------------+
    |                                            |
    | [Display:Form Step1]                       |
    +--------------------------------------------+
    
                       Submit
    
    +--------------------------------------------+
    | Headers: Step1 Form Data + set_query_var() |
    +--------------------------------------------+
    |                                            |
    | (get_query_var() = data found)             |
    |                                            |
    | [Display:Form Step2]                       |
    +--------------------------------------------+
    
                       Submit
    
    +--------------------------------------------+
    | Headers: Step2 Form Data                   |
    +--------------------------------------------+
    | (get_query_var() = empty)                  |
    |                                            |
    | [Display:"Succes!"]                        |
    +--------------------------------------------+
    

    The set_query_var() you’re seeing after the first submission is done by ACF Extended Form, during the Post Create Action. But there is no way to retrieve that data for the next submission, because Query Vars are just a variables (it’s not an input that is sent by the form).

    This behavior is perfectly normal, and TBH I’m sorry for giving you inaccurate information in my last answer. I’m kinda tired currently, as I’m working hard on ACF Extended 🙂

    But hey! There’s good news. There’s a way to make it work. Please note that you need to update ACF Extended to the latest 0.8.6.5 version, as it introduces the acfe_form_is_submitted() function, which will make your code more clean.

    Add this code in your page-form.php file:

    
    // Form: Names
    $formStep1Name = 'shopping-setup-competitors';
    $formStep2Name = 'shopping-setup-services';
    
    // Form: Step 1
    if(!acfe_form_is_submitted()){
        
        acfe_form($formStep1Name);
    
    // Form: Step 2
    }elseif(acfe_form_is_submitted($formStep1Name)){
        
        acfe_form($formStep2Name);
        
    // Success!
    }else{
        
        echo 'Success!';
        
    }
    

    In your functions.php file, add the following hook:

    
    // This hook let you alter the Form Arguments, and pass custom values too
    add_filter('acfe/form/load/form=shopping-setup-services', 'my_acf_form_step2_post_id', 10, 2);
    function my_acf_form_step2_post_id($args, $post_id){
        
        // Retrieve Query Var
        $step1_post = get_query_var('shopping-setup');
        $update_post_id = $step1_post['ID'];
        
        // Set the update post id
        $args['update_post_id'] = $update_post_id;
        
        return $args;
        
    }
    

    in your Step 2 Form, change the “Save Target” Template Tag to: {current:form:update_post_id}. You’ll recognize the custom argument we’ve added in the hook here. See screenshot: https://i.imgur.com/Nr1yirw.png

    I’ve tested it, and it works nicely. The cool part is that the actual update_post_id value is hidden inside encrypted ACF Form data, and visitors cannot change it manually, as it’s not displayed as plain text in the source code of the page.

    Side note: I’ve seen that you enabled the “Load Values” in your Step 2 Form, for the meta field “Services to Shop” (https://www.dropbox.com/s/7pwclmbltmto59t/Screenshot%202020-07-06%2008.05.23.png?dl=0). But unless you already have that data in your newly created post, it won’t display anything. You probably can disable the “Load Values” here.

    Have a nice day!

    Regards.

    Thread Starter Ian Chapman

    (@ianchapman)

    Thank you for confirming that the original instructions were not going to work.

    I’ve updated the plugin and modified the code to use the new function you mentioned. I can now report that the data is now saving for both steps of the form.

    Thanks again for all your help. I’m now going to head off and submit a 5-start review!

    Thanks,
    Ian

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello!

    I’m glad to hear it now works as expected.

    I’ll keep that post for people who want to setup multi-steps with ACFE Forms. As I mentioned earlier, I plan to add this feature within the UI in a future release to make it more easy for users, but this code will still work.

    Thanks for the kind review, it’s always much appreciated 🙂

    Have a nice day!

    Regards.

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

The topic ‘Multi-step form not saving data on second step’ is closed to new replies.