# Auto Submit Form with a script

**URL:** <https://community.hubspot.com/t/auto-submit-form-with-a-script/85970>\
**Category:** APIs & Integrations\
**Tags:** forms\
**Created:** [July 24, 2023, 1:30pm UTC](https://community.hubspot.com/t/auto-submit-form-with-a-script/85970 "2023-07-24T13:30:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CD20623](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@CD20623](https://community.hubspot.com/u/CD20623)\
**Post date:** [July 24, 2023, 1:30pm UTC](https://community.hubspot.com/t/auto-submit-form-with-a-script/85970/1 "2023-07-24T13:30:57Z")

</div>

Hi -

I need help with a hubspot form. I have a embedded form that I need to be able to auto submit with a script and the inline message to show up once the form has been submitted. I currently have a script in place to auto populate the email address field, I need to have the form auto submit when the page loads and the message to show up. I have read past posts but there isn’t a solution in any of them. Any help provided would be great. The customer would have already provided consent prior to submission. This is for an optout form. Thank you

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [July 24, 2023, 2:27pm UTC](https://community.hubspot.com/t/auto-submit-form-with-a-script/85970/2 "2023-07-24T14:27:21Z")

</div>

Hey @CD20623, is this a hubspot form hosted on a HubSpot landing page/website page? Or is it a custom from that you’ve created?

If it’s the former - you should just be able to use some Javascript to handle the form submit automatically when the page loads. There is a good example of how to do this [here](https://stackoverflow.com/questions/12376173/auto-submit-form-using-javascript) on Stack Overflow.

If it’s the latter you can use the [Submit form data endpoint](https://legacydocs.hubspot.com/docs/methods/forms/submit_form) to trigger the submission with whatever data you like.

---

<div class="post-metadata">

**Author:** ![CD20623](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@CD20623](https://community.hubspot.com/u/CD20623)\
**Post date:** [July 24, 2023, 2:33pm UTC](https://community.hubspot.com/t/auto-submit-form-with-a-script/85970/3 "2023-07-24T14:33:23Z")

</div>

It’s a hubspot form that is being hosted in a hubspot module. I have built a module that we can put on a page that will pull the email address out of the website url and pre-fill the email field, I’m having a issue with submitting the form automatically once the page is loaded in the browser.

---

<div class="post-metadata">

**Author:** ![coldrickjack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/coldrickjack/32/6684_2.png) [@coldrickjack](https://community.hubspot.com/u/coldrickjack)\
**Post date:** [July 24, 2023, 2:37pm UTC](https://community.hubspot.com/t/auto-submit-form-with-a-script/85970/4 "2023-07-24T14:37:57Z")

</div>

Hey @CD20623 ok thanks for clarifying. Next question is what specific issues are you seeing? Can you share the script you are currently using to attempt to produce the auto-submission?

---

<div class="post-metadata">

**Author:** ![CD20623](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@CD20623](https://community.hubspot.com/u/CD20623)\
**Post date:** [July 24, 2023, 4:29pm UTC](https://community.hubspot.com/t/auto-submit-form-with-a-script/85970/5 "2023-07-24T16:29:02Z")

</div>

\<script\>  
function getLastPart(url)  
{  
const parts = url.split(‘=’);  
return parts.at(-1);  
}  
var url = new URL(“{{ module.url.href }}/opt-out?email={{ module.contact\_email }}”);

window.addEventListener(‘message’, event =\> {  
if(event.data.type === ‘hsFormCallback’ && event.data.eventName === ‘onFormReady’) {  
$(‘input[name=“email”]’).val(“{{ module.contact\_email|replace(‘[’, ‘’)|replace(‘]’, ‘’) }}”);  
}  
});

console.log(url.href);  
console.log(url.search);  
console.log(url.searchParams.get(‘email’));  
\</script\>

{% form  
form\_to\_use=“{{ module.form\_field.form\_id }}”  
response\_response\_type=“{{ module.form\_field.response\_type }}”  
response\_message=“{{ module.form\_field.message }}”  
response\_redirect\_id=“{{ module.form\_field.redirect\_id }}”  
response\_redirect\_url=“{{module.form\_field.redirect\_url}}”  
%}
