• Resolved Zeff Coronel

    (@zeffcoronel)


    Hi, I’m having a problem with a form in forminator. In the behaviour tab I want to display a message based on the calculations, so I added an “if” condition in the html like this:

    <script type="text/javascript">
    var d = {calculation-8}
    
    if (d < 25) 
    {
    document.write("liviano")
    }
    else
    {
    document.write("sobrepeso")
    }
    </script>

    but every time I send the information in the form, it displays the code, not the condition.

    And if I select the visual preview I can see this image:

    https://monosnap.com/file/ipm75nY5AFlY63eElQWrxhDn7d1GtY

    I think it recognizes the script but can’t render.

    Any help would be greatly appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @zeffcoronel

    I hope you’re well today and thank you for your question!

    I’m afraid this currently wouldn’t be possible as the content of the filed is sanitized so the Javascript will be converted to a form in which is no longer “executable” (that’s why you see raw code.

    There’s also no “conditional logic” tools built-in into that submission message.

    A workaround for this would be to use redirect instead of the inline message.

    The way it would work would be that on the page to which you redirect you would need to read a query var (URL parameter) and then include your JS code in that page.

    Let’s say your query var is “calculation”.

    You would set redirect URL like

    yoursite.com/somepage?calculation={calculation-8}

    Then you’d need to read the “calculation” query var into variable, e.g. using JS similar to this:

    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const myCalc = urlParams.get('calculation');

    and then use “myCalc” in your script.

    Note please: I’ve not tested this JS code but I think it should do the trick. Alternatively, you could use a “mix” of JS and PHP directly in custom page template created for this “thank you” page and use core WordPress “get_query_var()” to fetch value from URL.

    Best regards,
    Adam

    Thread Starter Zeff Coronel

    (@zeffcoronel)

    Thank you so much! I really appreciate the suggestion, it works for the submission message, but I also want to display this conditional text in the email notification.

    I found this thread https://premium.wpmudev.org/forums/topic/forminator-pro-autofill-add-custom-field/

    And I’m creating an mu plugin that looks like this:

    <?php
    
    add_filter(
    
    'forminator_field_hidden_field_value',
    
    function( $value, $saved_value, $fields, $obj ) {
    
    if ( '%value1%' === $value ) {
    
    $fields = Forminator_API::get_form_field( 'formid', 'calculation-8', true );
    
    $value = $fields;
    
    }
    
    return $value;
    
    },
    
    99,
    
    4
    
    );

    I’m trying to give my hidden custom value, the value of a calculation so then I can assign the conditional text and then display in the submission message and email notification.

    It’s not working because it’s an array and if I run it like that, it just displays “array” instead of the value (calculation-8)

    Do you have any recommendation so I can successfully run this?

    Thanks in advance

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @zeffcoronel ,

    It’s not working because it’s an array and if I run it like that, it just displays “array” instead of the value (calculation-8)

    It is not working because get_form_field doesn’t return the value of the field, but properties of the field (like id, placeholder, etc etc).

    You need to again incorporate JS and PHP as Adam suggested there https://ww.wp.xz.cn/support/topic/javascript-not-rendering/#post-12836834 to gest that value to the variable.

    kind regards,
    Kasia

    Thread Starter Zeff Coronel

    (@zeffcoronel)

    Hi Kasia, thanks for the reply!

    Yes, I understand that I can use JS and PHP to be able to do that after submission but I was wondering how can I implement that for the email notification?

    I just want to display a “category” based on a calculation, for example if the result is less than 25, in the email to display “You are in x category” and if the result is over 25, in the email to display “you are in y category”

    I would appreciate it a lot if you have any suggestion

    Plugin Support Imran – WPMU DEV Support

    (@wpmudev-support9)

    Hello @zeffcoronel

    I trust you’re doing well!

    I apologize for teh delay in this thread. I’ve forwarded your question to our developers so they could have a closer look. Once there will be an update we will reply back in this thread.

    Kind regards,
    Nastia

    Thread Starter Zeff Coronel

    (@zeffcoronel)

    Hi Nastia! No worries, thank you so much for your patience and time 🙂

    Plugin Support Imran – WPMU DEV Support

    (@wpmudev-support9)

    Hello @zeffcoronel

    I hope all is well!

    Our developers suggested to use the following filter:

    add_filter(
        'forminator_custom_form_mail_admin_message',
        function( $message, $custom_form, $data, $entry ) {
            return $message;
        },
        10,
        4
    );

    The $message it the message content and the $data are the vars passed from the form. The best way to parse it would be adding some code that we will easily replace.

    For example, if we have such default notification:

    You have a new website form submission:
    {all_fields}
    ---
    This message was sent from {site_url}.

    Change it to:

    You have a new website form submission:
    
    %%category%%
    
    {all_fields}
    ---
    This message was sent from {site_url}.

    And than:

    add_filter(
        'forminator_custom_form_mail_admin_message',
        function( $message, $custom_form, $data, $entry ) {
            if ( false !== strpos( $message, '%%category%%' ) ) {
                $calculation = $entry->meta_data['calculation-1']['value']['result'];
                if ( ! empty( $calculation ) ) {
                    if ( $calculation < 25 ) {
                        $replace = 'liviano';
                    } else {
                        $replace = 'sobrepeso';
                    }
                }
    
                if ( ! empty( $replace ) ) {
                    $message = str_replace( '%%category%%', $replace, $message );
                }
            }
    
            return $message;
        },
        10,
        4
    );

    With this first we check if %%category%% exist.

    I hope this helps!

    Cheers,
    Nastia

    Thread Starter Zeff Coronel

    (@zeffcoronel)

    Thank you so much! This worked as a charm! I really appreciate your time and attention. I gave you a 5-star rating, you deserve it. I will recommend this plugin to all my colleagues 🙂

    Plugin Support Imran – WPMU DEV Support

    (@wpmudev-support9)

    Hello @zeffcoronel

    I hope you are doing well!

    Glad to know that all well now 🙂

    I’ve marked this ticket as resolved. If you do have any followup questions or require further assistance feel free to reopen it and let us know here.

    Kind regards,
    Nastia

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

The topic ‘Javascript not rendering’ is closed to new replies.