Need assistance with Forminator

I would like to show the results of my form on a separate page, is this possible?

  • Jonjon
    • Site Builder, Child of Zeus

    Dear Jerry,

    Thank you for reaching out to us!

    As Connor mentioned, you can achieve what you want by using the Forminator API.

    I see my colleague has created a plugin for you in the past which seems to work by adding the entry id to the redirect URL specified in the form settings.

    However, you would need to process that ID on the target page to extract data from the submission and display it. Which means you will need further custom development as well.

    I added a shortcode to the script that does just that. You can see a basic working example here:

    https://jonjon.wpmudev.host/2020/03/06/redirect-form/

    So I am sharing the script I used below of how you could use the Forminator API get_entry element in a shortcode(see attached image) and execute it in your target page to display the entry data. Please note that you will need quite some work to show it exactly as you want, but that is how it goes.

    Here is the link to our documentation: (https://wpmudev.com/docs/api-plugin-development/forminator-api-docs/#method-get_entry)

    
     <?php
    /**
     * Plugin Name: [Forminator] - Add entry id to redirect url
     * Description: [Forminator] - Add entry id to redirect url
     * Author: Thobk @ WPMUDEV
     * Author URI: https://wpmudev.com
     * License: GPLv2 or later
     */
    if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; }
    
    add_action( 'plugins_loaded', 'wpmudev_forminator_add_entry_id_to_redirect_url_func', 100 );
    
    function wpmudev_forminator_add_entry_id_to_redirect_url_func() {
    	if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) {
    		class WPMUDEV_Forminator_Add_Entry_ID_To_Redirect_URL{
    			private $form_id = 92;//enter form_id here
    			private $redirect_url = 'https://jonjon.wpmudev.host/2020/03/06/redirect-forminator-results/';
    			//enter your redirect url here, it must be match with the link you added on the form
    			private $entry_id;
    
    			public function __construct(){
    				add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'retrive_entry_id' ), 10, 2 );
    				add_filter( 'forminator_replace_form_data', array( $this, 'add_entry_id_to_redirect_url') );
    			}
    
    			public function retrive_entry_id( $entry, $form_id ){
    				if( $this->form_id == $form_id ){
    					$this->entry_id = $entry->entry_id;
    				}
    			}
    
    			public function add_entry_id_to_redirect_url( $content ){
    				if( $this->entry_id && $this->redirect_url === $content ){
    					$content = add_query_arg('entry_id', $this->entry_id, $content);
    				}
    				return $content;
    			}
    
    		}
    
    		$run = new WPMUDEV_Forminator_Add_Entry_ID_To_Redirect_URL;
    	}
    }
    
    // function that runs when shortcode is called
    function show_form_results() 
    { 
     
     
    $form_id = 92; //ADD YOUR FORM ID
    $entry_id=28; //ADD YOUR DEFAULT ENTRY ID, USE $_GET TO EXTRACT VALUE FROM URL
    
    //IF URL  CONTAINS ENTRY_ID 
    if(isset($_GET['entry_id']) && $_GET['entry_id'] >0) {$entry_id=$_GET['entry_id'];}
     
    $entry = Forminator_API::get_entry( $form_id, $entry_id );
    
    $array = json_decode(json_encode($entry), true);
    
    $data=$array['meta_data'];
    
    foreach($data as $key=>$e)
    {    
    	if($key!='_forminator_user_ip') $message.= $key . ' -> '.$e['value'] .'<br>';
    }
     
    // Output needs to be return
    return $message;
    } 
    // register shortcode
    add_shortcode('showFormResults', 'show_form_results'); 

    Hope that helps and please let me know if you have any questions.

    With Best Regards,
    Jonjon

  • Jerry
    • Site Builder, Child of Zeus

    Thanks Jonjon!
    I changed this

    private $form_id = 92;//enter form_id here
    			private $redirect_url = 'https://jonjon.wpmudev.host/2020/03/06/redirect-forminator-results/';
    			//enter your redirect url here, it must be match with the link you added on the form
    			

    to this

    private $form_id = 1986;//enter form_id here
    			private $redirect_url = 'http://nfk.jml.one/resultaat/';
    			//enter your redirect url here, it must be match with the link you added on the form
    			

    I changed this

    $form_id = 92; //ADD YOUR FORM ID
    $entry_id=28; //ADD YOUR DEFAULT ENTRY ID, USE $_GET TO EXTRACT VALUE FROM URL
    

    To this

    $form_id = 1986; //ADD YOUR FORM ID
    $entry_id=$_GET; //ADD YOUR DEFAULT ENTRY ID, USE $_GET TO EXTRACT VALUE FROM URL
    

    And added [ShowFormResults] to the page.
    But I can’t seem to get it to work.

    The plugin file name is forminator-add-entry-id-to-redirect-url.php and it is in the mu-plugins directory

    In the form the redirect url is http://nfk.jml.one/resultaat/

    What am I doing wrong?

    Many thanks!
    Jerry

  • Jonjon
    • Site Builder, Child of Zeus

    Dear Jerry,

    Thanks for your feedback.

    I noticed that indeed your shortcode and entry_id in link was not working so I connected FTP and found out why. The problem was your folder was named mu-plugin and it should be mu-plugins for WP to recognize it. I changed that, and now it is picking up the shortcode and adding entry id to redirect URL of your forms. (Please see screenshot below)

    [attachments are only viewable by logged-in members]

    I also went ahead and copied the result table and edited the values for you, to help you with the results table on target page. I changed the code for that as below:

    
     
    $form_id = 1968; //ADD YOUR FORM ID
    $entry_id=33; //ADD YOUR DEFAULT ENTRY ID, USE $_GET TO EXTRACT VALUE FROM URL
    
    //IF URL  CONTAINS ENTRY_ID 
    if(isset($_GET['entry_id']) && $_GET['entry_id'] >0) {$entry_id=$_GET['entry_id'];}
     
    $entry = Forminator_API::get_entry( $form_id, $entry_id );
    
    $array = json_decode(json_encode($entry), true);
    
    $data=$array['meta_data'];
    
    $result='<style>  
    table {
    	background-color:#174A5C !important;
    }
    table tr:nth-child(2n+1) {
       background-color:#174A5C !important;
    }</style>
    <table class="testResult" style="width: 80%;margin:15px;">
    <tbody>
    <tr>
    <th> </th>
    <th>Jouw Antwoord</th>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Wat is je leeftijd</span></p>
    </td>
    <td><strong>'. $data['date-1']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Wat is je sekse</span></p>
    </td>
    <td><strong>'. $data['radio-2']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Welke vorm van kanker heb je (gehad)?</span></p>
    </td>
    <td><strong>'. $data['select-1']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Ik heb momenteel (betaald) werk</span></p>
    </td>
    <td><strong>'. $data['radio-1']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Ik weet wat de gevolgen van de ziekte en/of behandeling voor mijn (toekomstige) werk betekenen</span></p>
    </td>
    <td><strong>'. $data['radio-3']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Ik verwacht steun van mijn omgeving (familie en vrienden/school)</span></p>
    </td>
    <td><strong>'. $data['radio-4']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Ik verwacht steun van mijn </span><span class="s2">werkomgeving (werkgever/collega’s)</span></p>
    </td>
    <td><strong>'. $data['radio-5']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Er is met mij gesproken over wat mijn ideeën en vragen zijn met betrekking tot werk (door de zorgverlener in het ziekenhuis)</span></p>
    </td>
    <td><strong>'. $data['radio-6']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Er is met mij gesproken over wat mijn ideeën en vragen zijn met betrekking tot werk (door familie/school/werkgever-collega’s/bedrijfsarts)</span></p>
    </td>
    <td><strong>'. $data['radio-7']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Ik weet waar of bij wie ik terecht kan voor vragen of ondersteuning</span></p>
    </td>
    <td><strong>'. $data['radio-8']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Ik heb een toekomstplan en weet welke stappen ik moet nemen</span></p>
    </td>
    <td><strong>'. $data['radio-9']['value'] .'</strong></td>
    </tr>
    <tr>
    <td>
    <p><span class="s1">Ik ben tevreden over mijn leven</span></p>
    </td>
    <td><strong>'. $data['radio-10']['value'] .'</strong></td>
    </tr>
    </tbody>
    </table>';
    
    /*foreach($data as $key=>$e)
    {    
    	//if($key!='_forminator_user_ip') $message.= $key . ' -> '.$e['value'] .'<br>';
    }*/
     
    // Output needs to be return
    return $result;
    } 

    Hope that helped and let me know if you have any further doubts.

    With Best Regards,
    Jonjon

  • Jerry
    • Site Builder, Child of Zeus

    Many thanks Jonjon,
    And apologies for the typo! I tested it and it worked! It is outputting the data.
    This said I should change the entry Id with $_GET to get the variable value but that doesn’t work yet.
    $entry_id=33; //ADD YOUR DEFAULT ENTRY ID, USE $_GET TO EXTRACT VALUE FROM URL

    I changed it to this
    $entry_id=$_GET; //ADD YOUR DEFAULT ENTRY ID, USE $_GET TO EXTRACT VALUE FROM URL

    Anything I have to change or do differently?

    Many thanks,
    Jerry

  • Jonjon
    • Site Builder, Child of Zeus

    Hi Jerry,

    No need to change that line. The line below that is the one verifying if a $_GET[‘entry_id’ ] is set and putting that value in the correct variable, in this case $entry_id. That value 33 is just a default value of a submission we know exists. But you should handle the case when no entry_id is provided in URL as well. So you can leave those three lines just like this:

    $form_id = 1968; //ADD YOUR FORM ID
    $entry_id=33; //ADD YOUR DEFAULT ENTRY ID IF you desire so.
    
    //IF URL  CONTAINS ENTRY_ID 
    if(isset($_GET['entry_id']) && $_GET['entry_id'] >0) {$entry_id=$_GET['entry_id'];}
     

    I went ahead and changed it back for you okay?

    Hope that helps.

    With Best Regards,
    Jonjon

  • Jerry
    • Site Builder, Child of Zeus

    Hi, I wanted to ask if it is possible to get the description of a radio box. I can get the label with $data[‘radio-3’][‘value’] but not the description. How would I do it? I’ve tried also $data[‘radio-3’][‘value’] [‘description’] but this gives a value back “E” where it should give a percentage back (100%, 80%, etc)

    Thanks!!

  • Pawel Pela
    • Ex Staff

    Hello Jerry !

    The default API when using Forminator_API::get_entry method doesn’t return the option names, only the selected value like this (output of the returned Forminator_Form_Entry_Model object from var_dump):

    ["radio-1"]=> array(2) { ["id"]=> string(1) "5" ["value"]=> string(3) "one" }

    However, you can get the descriptions by using another method, Forminator_API::get_form_fieldhttps://wpmudev.com/docs/api-plugin-development/forminator-api-docs/#method-get_form_field :

    Calling:

    $form_id = 123;
    $field_id = 'radio-1';
    $to_array = true;
    $result = Forminator_API::get_form_field( $form_id, $field_id, $to_array );

    For my form this returned an array and I could print the field options by accessing following items in the array:

    foreach($result["options"] as $option) {
        echo $option["label"];
    }

    The $result[“options”] array is int-indexed array of arrays that contain the following fields, each:

    $option[“label”] – name of the option displayed to visitors
    $option[“value”] – the actual value of the option when selected

    Hope this helps!

    BTW to ensure a prompt response, please create a new ticket for each new question :slight_smile: This will ensure you will get a faster response from our team and will make it easier to handle for us. It may be beneficial to others who are having a similar question – one post per question will greatly help in searching our forum for answers :slight_smile: Of course if you want to give us some additional context, please link back to the original ticket in the description :slight_smile:

    Kind regards,
    Pawel