• Resolved orjanmen

    (@orjanmen)


    Hi,

    I got a text field I use as a reference. I prefill this with URL parameter. It is important that the user don’t change this field. To help with that, I have made it hidden. But now I discovered that the hidden fileld don’t get populatet with value from URL.

    Any ways for me to make this work?

    Thanks.

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

    (@wpmudev-support8)

    Hi @orjanmen

    I hope you’re well today!

    I’m guessing you populate that hidden field using some code (probably JS), right?

    This would be happening due to the exact reason you want to use it – so user wouldn’t be able to change it. Hidden fields have a number of default options available in their settings and upon submission only those values would be saved. If the field has a different value than one of the values chosen in its settings, such value will not be saved.

    Otherwise, user could simply edit the field value in page source or override it using POST request.

    —-

    You mentioned you fill it with URL parameter. Is it the URL of the page that the form is on? If yes, hidden field has “Embed post/page URL” option available for it that you can use without any code.

    If that field is passed over to form via URL, then instead you can use option “Query var” of hidden field to put it in hidden field.

    Otherwise, you’ll need to use additional PHP code to keep the field value like this:

    add_filter('forminator_prepared_data', 'wpmudev_update_hidden_field_val_copy', 10, 2);
    function wpmudev_update_hidden_field_val_copy($prepared_data, $module_object)
    {
        $form_ids = array(1671); //Please change the form ID
        if (!in_array($module_object->id, $form_ids)) {
            return $prepared_data;
        }
    
        foreach ($prepared_data as $key => $value) {
            if (strpos($key, 'hidden-1') !== false) {
                $prepared_data[$key] = sanitize_text_field($_POST[$key]);
            }
        }
    
        return $prepared_data;
    }
    
    add_action('forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data_custom', 10, 3);
    function wpmudev_change_hidden_field_data_custom($entry, $module_id, $field_data_array)
    {
        $form_ids = array(1671); //Please change the form ID
        if (!in_array($module_id, $form_ids)) {
            return;
        }
    
        foreach ($field_data_array as $key => $value) {
            if (strpos($value['name'], 'hidden-1') !== false) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = 'YOUR CUSTOM VALUE HERE';
            }
        }
    }

    Note that you need to set your form ID in this code and hidden field ID in two places. Then you’d need to replace YOUR CUSTOM VALUE HERE with either your custom URL or a function that provides it.

    If you have any additional questions about it, let us know, please.

    Best regards,
    Adam

    Thread Starter orjanmen

    (@orjanmen)

    Hi,

    Thanks for your reply.

    I have used Query parameter, and for now, that is good enough, safety wise. Query parameter populates the field if it is visible, but don’t if it’s hidden.

    Until now I have hidden the field with two rules, each for one of the two options on a pre selected radio checkbox. I guess that is not the correct way to do it, but I’m not into this, and really only try to do it simple. Is it possible to hide the field with CSS classes?

    Thread Starter orjanmen

    (@orjanmen)

    EDIT: Now I just found the “Hidden field” option. I have been using a text field. Now it all makes sense. Thanks for your reply. I’ll mark this thread as resolved.

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

The topic ‘URL Parameter to hidden fields’ is closed to new replies.