APIs & Integrations

akanshmurthy
Member

Resetting form after submit using onFormSubmit callback

Why would this be? The API docs say the following: “Callback that executes after form is validated and is ready to submit. This is for any logic that needs to execute after the submit. Any changes will not be validated. Takes the jQuery form object: onFormSubmit($form).”

I’m trying to apply logic (clearing the form) that needs to be executed after the submit. But, the logic is being executed before the data is actually passed to HubSpot because all I see on the HubSpot contact list section are blank entries… If I comment out the onFormSubmit code, it works as expected.

Here’s the actual code too:
hbspt.forms.create({
portalId: ‘2333626’,
formId: ‘9a3ba836-f31f-4869-8d28-d264cc4259a8’,
onFormSubmit: function($form) {
$form[0].reset();
}
});

hbspt.forms.create({
portalId: ‘2333626’,
formId: ‘7ff1f00b-988c-4906-bd48-57ba6851b135’,
target: ‘.newsletter-form’,
onFormSubmit: function($form) {
$form[0].reset();
}
});

0 Upvotes
4 Replies 4
akanshmurthy
Member

Resetting form after submit using onFormSubmit callback

Ok, thanks. I’ll try that.

0 Upvotes
Dadams
HubSpot Employee
HubSpot Employee

Resetting form after submit using onFormSubmit callback

Hi @akanshmurthy

That wording is a little misleading. It’s probably more accurate to say that the the function fires when the form is ready to submit (which means all of the fields have passed validation), but just before the data is actually sent. I’ll get the wording updated to better reflect that.

We don’t currently have a hook that would fire after the form data is sent, and specifically for your case, we don’t have a way to clear the form since our forms are designed to redirect to a thank you page, or to replace the form with a thank you message, so in both cases the visitor wouldn’t see the form after submission. Can you tell me more about how you’re using the form and why you’d need to clear it?

0 Upvotes
akanshmurthy
Member

Resetting form after submit using onFormSubmit callback

@dadams, thanks for the response. Ok, thanks for updating the wording.

Well, the issue is that if we don’t clear the form and the user comes back to the website, the data is already filled out with their prior information. We’d like to clear it because it’s a bad user experience for the form to stay filled out even after it’s submitted. With the current implementation, the form gets cleared before the data is sent.

0 Upvotes
Dadams
HubSpot Employee
HubSpot Employee

Resetting form after submit using onFormSubmit callback

That’s a feature that’s built into our forms. We’ll automatically populate forms with data from their contact record, so that visitors won’t need to enter duplicate data. If you want to disable that, you can find the instructions for doing so here:

How do I change whether my forms pre-populate a users information?

Navigate to the forms tool to adjust this setting

0 Upvotes