APIs & Integrations

Not applicable

onFormSubmit - How do you send the form data via email and/or into MySQL or add to a text file?

SOLVE

Hi,

Being new to HS, I am really liking the embedded form feature and what you can do with it (see previous thread, for instance).

One thing I would like to do after the submit button is clicked and the data is validated is have the validated form field data sent to two additional locations using onFormSubmit. I would like to send it in an email somewhere, and also have it added to a MySQL db on the server hosting the page on which the embedded form appears. Or if the latter is not possible then at least have it appended to any other data already in a text file on the web server in a format that can easily be imported into a database or spreadsheet. What is the script or code to do that? I've been searching online, including other threads related to onFormSubmit, and have not found that. The field names and IDs I do have, however. I'm not sure if both are needed or only one:

Names:

firstname, lastname, email, phone

ID's:

firstname-nnnnnnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnnn
lastname-nnnnnnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnnn
email-nnnnnnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnnn
phone-nnnnnnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnnn

And if anyone knows only part of the answer since there is more than one goal here, I would much appreciate and be happy to receive whatever part you know. If it means having to call a server-side script on the web server, that's fine too.

Thanks.

onFormSubmit: function($form) {
// Stuff do do before form is submitted
}

0 Upvotes
1 Accepted solution
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

onFormSubmit - How do you send the form data via email and/or into MySQL or add to a text file?

SOLVE

Hi @John5,

  1. It will not be possible to leverage PHP directly within the form embed code to send an email. As the article you linked describes, however, it is theoretically possible to POST to your own mail server using AJAX. I can't recommend specific implementation steps to this end, but you could use a native HubSpot solution to fill a similar use case: You could create an automated email containing personalization tokens for each of the properties a contact submitted in the form, and then distribute the email through a workflow.

  2. As covered in this Quora discussion, mySQL data can't be modified client-side with HTML or JavaScript. Instead, you would need to use AJAX to POST the form data to your own server (similar to this post), where you would use your server-side language (e.g. PHP, Node.js, etc.) to format the data before sending it to your mySQL database.

  3. @Connor_Barley has seen something like this with node.js using the File System package, where you can use the appendFile method to append text or data to a TXT file. Like the above response, though, this would need to be handled on your own server's back end. Here's a video outlining the approach. That said, I can't speak to the viability of this given your existing system or how exactly you'd go about writing the code.

Isaac Takushi

Associate Certification Manager

View solution in original post

0 Upvotes
3 Replies 3
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

onFormSubmit - How do you send the form data via email and/or into MySQL or add to a text file?

SOLVE

Hi @John5,

  1. It will not be possible to leverage PHP directly within the form embed code to send an email. As the article you linked describes, however, it is theoretically possible to POST to your own mail server using AJAX. I can't recommend specific implementation steps to this end, but you could use a native HubSpot solution to fill a similar use case: You could create an automated email containing personalization tokens for each of the properties a contact submitted in the form, and then distribute the email through a workflow.

  2. As covered in this Quora discussion, mySQL data can't be modified client-side with HTML or JavaScript. Instead, you would need to use AJAX to POST the form data to your own server (similar to this post), where you would use your server-side language (e.g. PHP, Node.js, etc.) to format the data before sending it to your mySQL database.

  3. @Connor_Barley has seen something like this with node.js using the File System package, where you can use the appendFile method to append text or data to a TXT file. Like the above response, though, this would need to be handled on your own server's back end. Here's a video outlining the approach. That said, I can't speak to the viability of this given your existing system or how exactly you'd go about writing the code.

Isaac Takushi

Associate Certification Manager
0 Upvotes
Not applicable

onFormSubmit - How do you send the form data via email and/or into MySQL or add to a text file?

SOLVE

Anyone? If the email part can't be done directly or indirectly, I'll gladly take the part about adding into a text file or db.

0 Upvotes
Not applicable

onFormSubmit - How do you send the form data via email and/or into MySQL or add to a text file?

SOLVE

Is this a viable answer for the email part? Can I get this PHP into the "Stuff do do before form is submitted" instructions and have it run or a function that uses it:

<?php
$mailto = ???;
$mailFrom = ???;
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];

mail($mailto, $firstname, $lastname, $email, $phone, "From: ".$mailFrom);
?>

Or...

Would it have to be a combination of JQuery and Ajax (?):

0 Upvotes