Hello Developers!
We’ve embedded a HubSpot form into an ROI Calculator, but hidden fields are not being pre-populated with calculated values from JavaScript variables. We are kindly requesting for your advice on the following:
- Is there a recommended method to dynamically populate hidden fields in HubSpot forms?
- Should we use server-side pre-filling (e.g., via HubSpot API) instead of client-side JavaScript?
- Any known workarounds or best practices for this use case?
Here’s what we’ve tried:
- Calculator Inputs: Users enter values into input fields (e.g., number of applicants, hourly rate).
- Form Display: When the user clicks “Get My ROI Report”, the HubSpot form is displayed.
- Pre-Populate Fields: We use onFormReady to populate hidden HubSpot fields with both user inputs (e.g., applicants, hourly_rate) and calculated values (e.g., total_savings, roi_percentage).
HubSpot Form Embed Code:
<div id="leadForm">
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
hbspt.forms.create({
portalId: "48852168",
formId: "705dca6a-a671-4b60-8849-da3d06545295",
region: "na1",
onFormReady: function(form) {
// Pre-populate fields here
const applicants = document.getElementById("applicants").value;
form.find('input[name="number_of_applicants_"]').val(applicants);
// Example calculated value:
const total_savings = 10000; // Simplified for testing
form.find('input[name="total_savings__us_"]').val(total_savings);
}
});
</script>
</div>
Problem:
- User Inputs (e.g., applicants) are captured correctly, but hidden fields like number_of_applicants_ do not reflect these values.
- Calculated Values (e.g., total_savings) also fail to populate.
- The form submission does not include the pre-filled values.
What We’ve Tried
- onFormReady Callback: Used to ensure the form is fully loaded before setting values.
- Field Name Verification: Confirmed that the HubSpot field names (e.g., number_of_applicants_) match the portal’s form settings.
- Hardcoding Values: Even hardcoded values (e.g., total_savings = 10000) do not work, ruling out calculation logic errors.