• Resolved richardu

    (@richardu)


    I’m really loving the acfe plugin. Thanks!

    I’m using the same form multiple times on a page which works fine except that my Success Message shows up on all forms rather than only on the form that was submitted.

    How would I create unique ID for each form and then limit its Success Message to the same form.

    FWIW, I’m calling the form with a shortcode and I could add a parameter like this:
    [acfe_form name="test" form_number="x"] where x would be different each time I call the form.

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

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    I’m glad to hear that you enjoy ACF Extended ??

    What you’re describing is the normal Form behavior, as the form name is the real unique identifier. If you render the same form multiple time on the same page without Redirect Action, all of them will display the success message, because the page can only distinguish them by their name.

    You can however bypass that logic using the acfe/form/load hook (See documentation), and display the success message only if the $form['my_data'] correspond with the form that has been submitted using the acfe_is_form_success() helper (See documentation).

    Here is a usage example:

    add_filter('acfe/form/load/form=my-form', 'my_form_load', 10, 2);
    function my_form_load($form, $post_id){
        
        // get the updated message from the UI
        $updated_message = $form['updated_message'];
        
        // disable the sucess message for now
        $form['updated_message'] = false;
        
        // check if the form has been submitted
        if(acfe_is_form_success($form['name'])){
            
            // get the submitted form arguments
            $success_form = acfe_form_decrypt_args();
            
            // get 'my_data' from the current form & the submitted form
            $form_data = acf_maybe_get($form, 'my_data');
            $success_form_data = acf_maybe_get($success_form, 'my_data');
            
            // check that the submitted form has the same 'my_data'
            if($form_data && $success_form_data === $form_data){
                
                // re-assign the updated message on the current form
                $form['updated_message'] = $updated_message;
                
            }
            
        }
        
        // return
        return $form;
        
    }
    

    Front-end PHP code:

    acfe_form(array(
        'name' => 'my-form',
        'my_data' => '1'
    ));
    
    acfe_form(array(
        'name' => 'my-form',
        'my_data' => '2'
    ));
    
    acfe_form(array(
        'name' => 'my-form',
        'my_data' => '3'
    ));
    

    Video demo of the result.

    Hope it helps!

    Have a nice day!

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Unique form id for Success Message’ is closed to new replies.