Skip to content

Instantly share code, notes, and snippets.

@mfrancois3k
Forked from stevedrobinson/mautic-form-preload.js
Last active September 7, 2022 18:28
Show Gist options
  • Save mfrancois3k/6387eda9d08bc9d6d0a1da2dc83b53de to your computer and use it in GitHub Desktop.
Save mfrancois3k/6387eda9d08bc9d6d0a1da2dc83b53de to your computer and use it in GitHub Desktop.
Pre-populate Mautic Form Data from Query String Parameters
Hi. I have read the threads on this and would love to try out this patch. I am not a developer. I can find my way around code but have no idea where I should put this file. Could you let me know.
Thanks
Well, Though late, it may be useful....
One Option is You should add this file in the theme folder...
Create a file /themes/<your_landingpage_theme_folder>/js/mautic-form-preload.js
Edit File /themes/<your_landingpage_theme_folder>/html/base.html.twig
<HTML>
<head>
....
</head>
<body>
.......
</body>
<script type="text/javascript" src="{{ getAssetUrl('themes/'~template~'/js/mautic-form-preload.js')}}"></script>
</html>
Voila! Start populating your Pages & Forms
(function(document){
function populateForms(){
if (document.readyState == 'interactive') {
if (document.forms.length !== 0 && location.search) {
var query = location.search.substr(1);
query.split('&').forEach(function (part) {
if (part.indexOf('=') !== -1) {
var item = part.split('=');
var key = item[0];
var value = decodeURIComponent(item[1]);
var inputs = document.querySelectorAll('*[name^="mauticform[' + key + ']"');
inputs.forEach(function (input) {
if(input.type==='checkbox' && input.value===value){
input.checked=1;
} else {
input.value = value;
}
});
}
});
}
}
}
document.addEventListener('readystatechange',populateForms);
populateForms();
}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment