Include form fields as redirect URL parameters on an embedded form

SOLVE
Highlighted
Occasional Contributor

I need to embed a HS form on an external (not CoS) website. And when the user submits the form, the redirect URL needs to include form fields as parameters.

Is this possible?

We figured out a way of doing this on a CoS landing page using a native HS form, but it was pretty tricky. In this case, I need to embed the form on an external page rather than use a CoS page.

What I’m trying to accomplish is capture the lead’s info (name, company, email) and then pass that info off to a scheduling app which can use parameters to pre-fill a scheduling form so the user doesn’t have to re-enter their info. And I want to do it through a HS form first so that we capture the lead and source information in HS. If we did it straight in the scheduling app then we could create a lead using Zapier but without Source info.

Every other form app I’ve used has been able to do this. Still surprised HS can’t do it off the shelf. But I hope someone has a creative workaround. Thanks.

2 Accepted solutions

Accepted Solutions
Esteemed Contributor | Diamond Partner

You could achieve this by using the onFormSubmit callback and switching the form to use an inline message instead of a redirect (explained below).

hbspt.forms.create({
    portalId: 'xxxxxxxx',
    formId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    inlineMessage: 'Your submit message here',
    onFormSubmit: function($form){
        setTimeout( function() {
            var formData = $form.serialize();
            window.location = "http://www.yoururl.com?" + formData;
        }, 250 ); // Redirects to url with query string data from form fields after 250 milliseconds.
    }
});

Because you have access to the jQuery $form object inside the onFormSubmit listener you can serialize the forms data and append it to a url to redirect to after the form is submitted. This only works if you have the inlineMessage set and disable the redirectUrl for the form.

See if that helps you out.

Esteemed Contributor | Diamond Partner

@Albo 

 

Try setting your setTimeout to something higher than 100 milliseconds and see if that helps. I haven't had or seen this issue before - and in theory it shouldn't because the onFormSubmit is processed after the form is validated and request is sent. 

 

My theory is that if you redirect too quickly it may terminate the XHR request over to HubSpot's servers. I haven't actually tested this issue myself yet though - let me know what you find.

Reply
0 Upvotes
14 Replies 14
Esteemed Contributor | Diamond Partner

You could achieve this by using the onFormSubmit callback and switching the form to use an inline message instead of a redirect (explained below).

hbspt.forms.create({
    portalId: 'xxxxxxxx',
    formId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    inlineMessage: 'Your submit message here',
    onFormSubmit: function($form){
        setTimeout( function() {
            var formData = $form.serialize();
            window.location = "http://www.yoururl.com?" + formData;
        }, 250 ); // Redirects to url with query string data from form fields after 250 milliseconds.
    }
});

Because you have access to the jQuery $form object inside the onFormSubmit listener you can serialize the forms data and append it to a url to redirect to after the form is submitted. This only works if you have the inlineMessage set and disable the redirectUrl for the form.

See if that helps you out.

New Member

This worked great for me, just wanted to say thanks!

Reply
0 Upvotes
New Contributor

Could someone help me figure out why @derekcavaliero's solution is not working? I implemented his solution on the page https://www.krister.com/language-of-leadership-assessment

 

The inline message is appearing when I submit the form, but the page is not redirecting to the page that I put into the window.location. 

 

Here's the code that I put into Squarespace.  I bolded the changes I made to your solution:

!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
  hbspt.forms.create({
    portalId: "xxxx",
    formId: "xxxxx",
    inlineMessage: 'Your submit message here',
    onFormSubmit: function($form){
        setTimeout( function() {
            var formData = $form.serialize();
            window.location = "https://www.krister.com/success-language-of-leadership-assessment?" + formData;
        }, 250 ); // Redirects to url with query string data from form fields after 250 milliseconds.
    }
});
</script>

 

Reply
0 Upvotes
HubSpot Moderator

Hey, @Kristeru.

 

I'm pretty sure this is because your form is loading in an iframe.

 

I see that you have one of the new "themes" enabled here in the form editor's Style & preview tab. This causes the form to render in an iframe, so I'd recommend reverting to the "old theme" if you need to customize the embed code.

Isaac Takushi

HubSpot Developer Support
Reply
0 Upvotes
Occasional Contributor

@derekcavaliero 

 

Can you see any reason why this modifying the HubSpot form embed script this way would interfere with how HubSpot receives the data upon submission?

 

For whatever reason, when I use this method on a Firefox browser the contact is not passed back to HubSpot. Chrome and Safari both function as intended. The default (non-modified) script works fine in Firefox.  All are standard browsers with default configurations, *not* in incognito.

 

<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
hbspt.forms.create({
portalId: "xxxxxxxx",
formId: "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
inlineMessage: 'Just a moment please',
onFormSubmit: function($form){
setTimeout( function() {
var formEmail = $form.find('input[name="email"]').val();
window.location = "https://www.xxxxx.com/xxxx/?email=" + encodeURIComponent( formEmail );
}, 100 ); // Redirects to url with query string data from form fields after 100 milliseconds.
}
});
</script>

 

Thank you!

 

Reply
0 Upvotes
Esteemed Contributor | Diamond Partner

@Albo 

 

Try setting your setTimeout to something higher than 100 milliseconds and see if that helps. I haven't had or seen this issue before - and in theory it shouldn't because the onFormSubmit is processed after the form is validated and request is sent. 

 

My theory is that if you redirect too quickly it may terminate the XHR request over to HubSpot's servers. I haven't actually tested this issue myself yet though - let me know what you find.

Reply
0 Upvotes
Occasional Contributor

@derekcavaliero You're spot on! Setting the redirect back to 250 ms resolved the issue. Thank you!

HubSpot Employee

Hi @Lloyd_Silver,
Another way to do this is by re-running the hbspt.forms.create method of the embed code on change to each field and recreating the redirectUrl, appending each field. This changes the context and rebuilds the form on each call. @derekcavaliero 's method is probably better, but there are multiple ways to proceed.

That method looks like this:

hbspt.forms.create({
portalId: ‘[hub_id]’,
formId: ‘[form_guid]’,
redirectUrl:‘https://www.domain.com/page’,
target:’#target-node-for-form-build
});

In your JS, recall that entire method on change to each field, creating a redirectUrl like:
redirectUrl:‘https://www.domain.com/page?firstname=Lloyd’. Be sure to include the ‘target’ node so the embed code knows where to rebuilt the form each time.

Reply
0 Upvotes
Occasional Contributor

Hi,

I've the same issue as the same @lloydsilver . The thing is I want to redirect users to a dynamic URL which already includes a "?".

 

Basically I want our users to fill a form with their website as one property to be redirected to a URL which include their website.

So if I a fill a form putting: http://www.mywebsite.com

People would be redirected to the following URL http://www.domain.com/simulate.php?url=http://www.mywebsite.com

 

1. I'm not sure to really understand how to use target:’#target-node-for-form-build’

2. I tried the dynamic query string (as i saw in HS knowledge) but it didn't work neither and i've the feeling it's because of the "?" which is inside the URL.

3. I tried the @derekcavaliero method but i didn't get how to use  window.location = "http://www.yoururl.com?" + formData;

 

Thanks in advance for your help

Reply
0 Upvotes
HubSpot Moderator

Hi, @Gthn.

 

Let's continue the conversation in this thread.

Isaac Takushi

HubSpot Developer Support
Reply
0 Upvotes
New Member

Hi @Lloyd_Silver,

I'm having the same issue, but on a COS landing page.
As you've apparently solved it before, any chance you can share the solution here?

Thanks a lot!

Reply
0 Upvotes
Top Contributor

Hey @Jeromecollomb, I know it's been a while since you asked your question, but I just posted an answer to another question that might help you out.

@derekcavaliero you are the man. This answer saved me a lot of time!

Occasional Contributor

YOU REALLY ARE THE MAN!@!!! This is it. 

Esteemed Contributor | Diamond Partner

@john_ascend @Trevor_Hatfield

Happy to help!

One thing I did not mention in my solution was to make 100% sure that you exclude any PII in the form data from being collected by Google Analytics.

Since we're talking about appending the data to the URL via a query string - GA will automatically capture that data inside its pageview reports. This means you'll likely need to add some custom logic around your tracking to make sure you adhere to the GA terms of use regarding collection of PII.

If you're using GTM (Google Tag Manager) to implement your web tracking (if you aren't I highly suggest you investigate) - this article is a great resource for understanding how to exclude data from being collected:

#GTMTips: Remove PII From Google Analytics Hits

Use customTask to automatically strip all personally identifiable information (PII) from requests sent to Google Analytics from your website.