Submit a form

Hi,
I have all my code working correctly. Thank you to all for your help.

I wonder if there is a way (code) to submit a form without using the Submit option of a button?

Thank you!

Pierre

Hi,
You can submit through code by calling the save function of the dataset. Look at the dataset documentation in the Wix Code API reference.

Tomer thank you for your response.
When I save the form dataset using the save function, it does save it to this form dataset but not to the Collection database. To do this, I use the Submit setup of a button then it goes to the Collection. I just would like to know how to do this with a function.

Pierre Lacasse

The dataset is just a “bridge” to the collection, it will always save to the collection unless there’s an error.
Internally, the submit action you set using the “Connect to data” panel is actually calling the same “save” function you cal call from code. So I suspect there’s a different problem there.
Are you calling save on the correct dataset? Can you maybe share the code you’re using? Or the entire site where it doesn’t work so we can take a look?
Thanks!

Tomer,
below my code.
This page has 5 input fields which I want to make sure people are filling them before accessing my site in order to collect their profile. Depending on the result, I either show or hide 2 buttons (update and to the web site which has the Submit function attached to it.

The code has 3 sections:
1- onReady checking on the 5 input fields
2- button 6 - to submit and go to the Accueil page
3- button 1 - for users to make an update

dynamicDataset is the db that collects the 5 input fields
ListeMembres is the collection db

import wixLocation from ‘wix-location’;
$w.onReady( function () {
$w(“#dynamicDataset”).onReady( () => {
$w(“#button1”).show();
$w(“#button6”).hide();
$w(“#vectorImage2”).hide();
$w(“#vectorImage3”).hide();
$w(“#vectorImage4”).hide();
$w(“#vectorImage5”).hide();
$w(“#vectorImage6”).hide();
var formvalid = 1;
var txtErrorOut = “”;
var nfamille = $w(“#input7”).value;
if (nfamille.length < 2) {
formvalid = 0;
txtErrorOut = “…Nom de famille trop court… “;
$w(”#vectorImage2”).show();
} else {
$w(“#vectorImage2”).hide();
}
var prenom = $w(“#input6”).value;
if (prenom.length < 2) {
formvalid = 0;
txtErrorOut = “…Prénom trop court… “;
$w(”#vectorImage3”).show();
} else {
$w(“#vectorImage3”).hide();
}
var dernierposte = $w(“#input4”).value;
if (dernierposte.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez votre dernier poste chez IBM… “;
$w(”#vectorImage4”).show();
} else {
$w(“#vectorImage4”).hide();
}
var anneedepart = $w(“#input5”).value;
if (anneedepart.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez l’année de départ d’IBM…”;
$w(“#vectorImage5”).show();
} else {
$w(“#vectorImage5”).hide();
}
var lieuactuel = $w(“#input8”).value;
if (lieuactuel.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez ville/province/pays actuels…”;
$w(“#vectorImage6”).show();
} else {
$w(“#vectorImage6”).hide();
}
if (formvalid === 1)
{
$w(“#button6”).show();
$w(“#text9”).text = “-”;
//$w(“#dynamicDataset”).save();
//(“#dynamicDataset”).submit();
//wixLocation.to(“/accueil”);
} else {
$w(“#text9”).text = txtErrorOut
$w(“#button6”).hide();
$w(“#button1”).show();
}
})
});
export function button6_click_1(event, $w) {
var formvalid = 1;
var txtErrorOut = “”;
var nfamille = $w(“#input7”).value;
if (nfamille.length < 2) {
formvalid = 0;
txtErrorOut = “…Nom de famille trop court… “;
$w(”#vectorImage2”).show();
} else {
$w(“#vectorImage2”).hide();
}
var prenom = $w(“#input6”).value;
if (prenom.length < 2) {
formvalid = 0;
txtErrorOut = “…Prénom trop court… “;
$w(”#vectorImage3”).show();
} else {
$w(“#vectorImage3”).hide();
}
var dernierposte = $w(“#input4”).value;
if (dernierposte.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez votre dernier poste chez IBM… “;
$w(”#vectorImage4”).show();
} else {
$w(“#vectorImage4”).hide();
}
var anneedepart = $w(“#input5”).value;
if (anneedepart.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez l’année de départ d’IBM…”;
$w(“#vectorImage5”).show();
} else {
$w(“#vectorImage5”).hide();
}
var lieuactuel = $w(“#input8”).value;
if (lieuactuel.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez ville/province/pays actuels…”;
$w(“#vectorImage6”).show();
} else {
$w(“#vectorImage6”).hide();
}
if (formvalid === 1)
{
$w(“#button6”).show();
$w(“#text9”).text = “-”;
$w(“#dynamicDataset”).save();
wixLocation.to(“/accueil”);
} else {
$w(“#text9”).text = txtErrorOut
$w(“#button6”).hide();
$w(“#button1”).show();
}
}
export function button1_click_1(event, $w) {
var formvalid = 1;
var txtErrorOut = “”;
var nfamille = $w(“#input7”).value;
if (nfamille.length < 2) {
formvalid = 0;
txtErrorOut = “…Nom de famille trop court… “;
$w(”#vectorImage2”).show();
} else {
$w(“#vectorImage2”).hide();
}
var prenom = $w(“#input6”).value;
if (prenom.length < 2) {
formvalid = 0;
txtErrorOut = “…Prénom trop court… “;
$w(”#vectorImage3”).show();
} else {
$w(“#vectorImage3”).hide();
}
var dernierposte = $w(“#input4”).value;
if (dernierposte.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez votre dernier poste chez IBM… “;
$w(”#vectorImage4”).show();
} else {
$w(“#vectorImage4”).hide();
}
var anneedepart = $w(“#input5”).value;
if (anneedepart.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez l’année de départ d’IBM…”;
$w(“#vectorImage5”).show();
} else {
$w(“#vectorImage5”).hide();
}
var lieuactuel = $w(“#input8”).value;
if (lieuactuel.length < 2) {
formvalid = 0;
txtErrorOut = “…Entrez ville/province/pays actuels…”;
$w(“#vectorImage6”).show();
console.log (“ici”)
} else {
$w(“#vectorImage6”).hide();
}
if (formvalid === 1)
{
$w(“#button6”).show();
$w(“#text9”).text = “-”;
$w(“#dynamicDataset”).save();
} else {
$w(“#text9”).text = txtErrorOut
$w(“#button6”).hide();
$w(“#button1”).show();
}
}

Did you make sure your code reaches the save function? You can check either with printing logs (using console.log function) or debugging the code using the browser developer tools.

I think I did but will verify again

Hi Tomer,
finally you are right, the save function is the same as the Submit option on a button. I must have been looking at the Sandbox instead of the Live folder on my Collection

Thank you for your great help!

Pierre

Glad it all worked out, have fun coding :slight_smile: