Add current URL in hiddent form input

Your software
My Mautic version is: 4.9 (official docker image)

Your problem
I want to create a custom form to add in multiple pages. Then I’d like to automatically add the page url (or title) in the submited data.

Is there any tricks to do that ? Builtin feature or custom javascript on form submit event ?

Hi Jeckel,

Welcome to the Mautic forum!

I came across an article that I believe could be quite helpful to you: Tracking Visitor Data by Smart URL

While using a custom form theme allows you to insert JavaScript, you can also achieve this with a form field called “HTML Area”. That field provides you with the flexibility to include scripts and other HTML elements.

I hope you find this information helpful and that it assists you with your problem!

2 Likes

Thanks a lot, it seems to fit what I need, I will try this and leave a comment here if it works (and how I made it works)

You can place a HTML field in the mautic form with this content:

<script>
(function(global, document) {

    function load() {

        if(!window.MauticFormValidations) return setTimeout(load, 50);
    
        //Config field name and form selector 
        var formSelectorName = 'form';
        var fieldName = 'form_url';
    
        var mauticForms = document.querySelectorAll(formSelectorName);
        for (i = 0;i<mauticForms.length; ++i) 
        {
            var formId = mauticForms[i].getAttribute('data-mautic-form');
            if(!formId)
            {
                continue;
            }
    
            var formField = document.querySelector("form#mauticform_"+formId+" [name=mauticform\\["+fieldName+"\\]");
            if(!formField)
            {
                continue;
            }
    
            if (typeof window.MauticFormCallback == 'undefined') {
                window.MauticFormCallback = {};
            }
    
            MauticFormCallback[formId] = {
                onValidateEnd: function (formValid) {
                  formField.value = window.location.href;
                }
            };  
    
        }
                
    }

    //if document is already ready: start if not: install event handler 
    "loading" === document.readyState ? document.addEventListener("DOMContentLoaded", load) : load(); 
    
}(window, document));
</script>

It should set the field “form_url” with the current site url

2 Likes