Java Script Snippet to post on an invisible field on a form

  • This is the code that creates the affiliate code → we need to take this into an invisible field Screenshot by Lightshot
  • There are hidden fields on all forms that captures affiliate codes
  • When a visitor comes onto the website the above JS creates a unique code.
  • When that visitor fills out a form I want that unique code to pass to a field in hubspot when the contact is created.
  • I have no idea how to accomplish this

Hi @RNelson312,

technically you can’t pass JS values to Hubl since Hubl is serverside and JS is clientside. Passing Hubl to JS on the other hand works.

However - you have several approaches:

The easiest one would be to create a custom module with a modified embed code like this

If you want to have always the same form you can grab the embed code of the form and paste it into the module.

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
 hbspt.forms.create({
 region: "na1",
 portalId: "#######",
 formId: "#########"
 });
</script>

to add some logic for the “after submit” logic, take a look at the advanced form options

What you can do is adding a JS script to the “onFormSubmit” or "onFormSubmitted"option like this

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
 hbspt.forms.create({
 region: "na1",
 portalId: "#######",
 formId: "#########", 
 onFormSubmit: {# your logic goes here #}
 });
</script>

Note: “onFormSubmit” triggers something directly after the submit-button is pressed, “onFormSubmitted” on the other hand triggers something after the form was send.

If you want to have different forms selectable in the module, simply add a form function to the module and modify the embed code like this:

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
 hbspt.forms.create({
 region: "na1",
 portalId: "#######",
 formId: "{{ module.form.form_id }}", 
 onFormSubmit: {# your logic goes here #}
 });
</script>

After you’ve written the module, save it and drop it into your page

Done

Another way could be to create workflows which add a generated code to each contact who fills out a or specific form

best,

Anton