Need to send form results to 3rd party service

Hi there!

I’m trying to create a simple form to collect first and last name, email, and mobile number.
This is the HTML version of the form:

<form method="post" action="https://app.flocknote.com/group/xxxxx/addToGroupFromOutside" target="_blank"> <input type="text" name="fname" placeholder="First Name" />	 <input type="text" name="lname" placeholder="Last Name" /> <input type="email" name="email" placeholder="Email Address" /> <input type="tel" name="mobile_phone" placeholder="Mobile Phone" /> <button type="submit" value="submit">Sign Me Up</button> </form>

I would like to use Wix Code to make the form blend with the rest of the web site.

Thanks!

Hi Miguel,

Welcome to WixCode.

See the article Accessing 3rd Party Services for more information on that subject.

As far as your form, there really is no need to create your own form in HTML. Everything you need to create a beautiful form can be easily done right inside of the Wix Editor.

In those rare cases where you really, really need to use HTML, see the article Working with the HTML Component in Wix Code for more information.

Have fun,

Yisrael

Thanks for the reply, Yisrael!

  1. I created 4 text inputs and a submit button. I also created a dataset (I’m not sure that was necessary). I connected all inputs and submit button to the dataset.

  2. I created a backend web module “aModule.js” with the following code:

import {fetch} from ‘wix-fetch’;

export function sendToFlocknote(data) {
fetch(“Flocknote”, {
method: ‘post’,
body: data
});
}

  1. Finally, in the Site Code on the same page as the form, I added an event handler that runs each time a new item is successfully submitted.

import {sendToFlocknote} from ‘backend/aModule.jsw’;

$w.onReady(function () {
$w(“#dataset1”).onAfterSave(sendToFlocknote);
});

export function sendFormData() {

var data = { 
	"fname":$w("#input1").value, 
	"lname":$w("#input2").value, 
	"email":$w("#input3").value, 
	"mobile_phone":$w("#input4").value 
}; 
	
sendToFlocknote(data); 

}


It’s not working. The form is supposed to do the following:

Sign Me Up

Hi Miguel,

You mentioned you have a dataset but nor sure if you need it.
$w(" #dataset1 ").onAfterSave only runs when dataset save (to database) is completed. In that case, you should make sure you do save (permission issues probably).

If you don’t really need that to be saved to the site database, I suggest creating a button that upon click, send the dat to the 3rd party app.
See button.onClick .

Liran.

Thanks Liran.

You are right. I don’t need the data saved in the site databased. I tried your suggestion, but it is still not working. I left the backend web module the same and changed the Site Code on the same page as the form to:

import {sendToFlocknote} from ‘backend/aModule.jsw’;

$w.onReady(function () {
$w(“#button3”).onClick(sendToFlocknote);
});

export function sendFormData() {

var data = { 
	"fname":$w("#input1").value, 
	"lname":$w("#input3").value, 
	"email":$w("#input2").value, 
	"mobile_phone":$w("#input4").value 
}; 

// Process request 
sendToFlocknote(data); 

}


Not working. :frowning:

You setting ‘sendToFlocknote’ as the onClick,
I think you meant:

$w.onReady(function () {
  $w("#button3").onClick(sendFormData);
});

Liran.

Yes. I caught that mistake after I replied to you. But it is still not working :frowning:

I didn’t include the target=“_blank” from the original HTML form. Could it be something related to that?

I don’t think so… target=“_blank” means that that a new tab will be opened.
Are you seeing any console errors?

No console errors.

I’ve tried inserting the HTML code in the website.

Sign Me Up

It works. When I click on the Submit button, the browser opens a new tab and directs me to Contact Flocknote where I can continue the registration process.

Nothing happens when I run the form I created with Wix using the above Wix Code.

Ok now I get it…
You’re actually redirecting to a page on submission.
Using ‘fetch’ from Wix Code API is used for REST APIs, and not for loading a web page.
This means that you can fetch ’ https://app.flocknote.com/group/198082/addToGroupFromOutside '.

You need to check if flocknote had a REST API (according to this , they do have), and make sure it fits your needs.

Liran.

Well, all I’m trying to do is to replicate what the HMTL embedded form does, which I know it works. Is there a way to do that with Wix Code?

Hey Miguel,

I think that REST/fetch is your best option (they do seem to offer support for this), but if you’re having trouble getting that working, you can perhaps embed the external page in your own page using $w.HtmlElement . For more information on how it’s used you can read Working with the HTML Component in Wix Code .

Keep us posted on your progress.

Yisrael

So can I pass the data from the input text fields created with Wix to the HTML component?

I guess I would need to “hide” from view the HTML component. How would I do that?

I’m guessing that I would need to use this code to pass the data from one of the input fields to the HTML component:

export function button3_onClick() {
// send message to the HTML Component
$w(“#html3”).postMessage($w(“#input1”).value);

How do I do the same for the rest of the inputs? How about the Submit button? And how do I match the input field in the Wix form to the HTML fields in the HTML component?

Hi Miguel,

Upon clicking the Submit button, the onClick() function of the Submit button would create an object of input values and do a postMessage to HtmlElement . When the HtmlElement receives these values, it would build the same URL as the form 's post action would and redirect to that URL.
Whether you hide the HtmlElement or not depends on how you want things to look and how they work. You can always hide it when the page is loaded, and show it when needed.

However - I still think your best bet is REST/fetch .

A happy new WixCode year,

Yisrael

I have the same challenge.
The proposed solution seems awfully complex and convoluted to achieve a pretty standard website functionality. Could anyone at Wix provide an example of how Wix Code and submit data from some input fields added via the Wix Editor, to an external website and redirect to that website? In other words, how to replicate a simple HTML Form submit action via Wix Code?

Thanks in advance,
Ben

Hi, I am currently experiencing the same problem that Benjamin described. Would it be possible to have a simple example ?
A.

Hi Benjamin, please do share if you have found a solution.
Thanks,
A.

An example of a simple form:

Basic Form

Create a basic form without writing a line of code. Use input elements, a dataset, and a button to capture and store user input in a database collection.

You can then transfer the form data to an external service using some of the techniques illustrated in some of the Advanced Examples .

Hello everyone, can anyone help me with my code? I’m unable to send my Form’s input to the external CRM.

$w . onReady ( function () {

});

import { fetch } from ‘wix-fetch’ ;

export function submit_click ( event , $w ){
let crmApiURL = “CRM Link”
let apiQuery = ‘’ ;
if ( $w ( “#fname” ). value !== null && $w ( “#fname” ). value . length > 0 ) {
crmApiURL += ‘&fname=[’ + $w ( “#fname” ). value + ‘]’ ;
}

if ( $w ( “#phone” ). value !== null && $w ( “#phone” ). value . length > 0 ) {
crmApiURL += ‘&phone=[’ + $w ( “#phone” ). value + ‘]’ ;
}

if ( $w ( “#email” ). value !== null && $w ( “#email” ). value . length > 0 ) {
crmApiURL += ‘&phone=[’ + $w ( “#email” ). value + ‘]’ ;
}

let fullURL = crmApiURL + apiQuery
fetch ( fullURL , { method : ‘post’ })
. then (() => {
})
}