Skip to content

Instantly share code, notes, and snippets.

@stevedrobinson
Forked from shawncarr/mautic-form-preload.js
Last active June 15, 2023 20:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stevedrobinson/66aa33f3f26ee81e80ae49fd89390713 to your computer and use it in GitHub Desktop.
Save stevedrobinson/66aa33f3f26ee81e80ae49fd89390713 to your computer and use it in GitHub Desktop.
Pre-populate Mautic Form Data from Query String Parameters
(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));
@ShareBtech
Copy link

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

@satsangswami
Copy link

satsangswami commented Sep 9, 2020

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...

  1. Create a file /themes/<your_landingpage_theme_folder>/js/mautic-form-preload.js
  2. 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>
  1. Voila! Start populating your Pages & Forms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment