Passing hidden data in a form

Hey guys, I’m pretty stoked about how this website is turning out so far. So to explain, I’ve created a form to add new CPR classes to our list to make it as easy as possible and so that we don’t have to go into the editor each time and blah blah… You can see it (and play around with it) at https://www.safetyinstructorgroup.com/create-classes and you can find the pages it passes data to at https://www.safetyinstructorgroup.com/Classes and then select the city for which you created a class. Feel free to add a class or two as we’re just testing things right now anyway. I will put this page behind an admin password when we go “live”. Once a class is created, I want to also pass an address linked to each city (Lynchburg, Verona, Bristol) without that address being seen by the user. So when the user selects “Lynchburg” I want to also pass hidden element that includes an address for Lynchburg to the database as well. I’m not sure if it’s possible? Seems like it should be as robust as this Wix Code platform has become. Thanks in advance for any help with this matter.

Hay Will,

It is actually quite easy. Just use the dataset onBeforeSave hook.

e.g.

$w('#dataset1').onBeforeSave(() => {
  $w('#dataset1').setFieldValue('address', <the address value>);
});
1 Like

Thanks Yoav, that looks like exactly what I need!

Ok, I think I’m on the right track but I haven’t been able to quite make it work. Here’s what I’ve come up with so far:

$w(‘#dataset1’).onBeforeSave(() => {
if ($w(‘#SelectCity’).value === ‘Lynchburg’) {
$w(‘#dataset1’).setFieldValue(‘address’, ‘1007 Sheffield Drive Lynchburg, VA 24502’);
} if ($w(‘#SelectCity’).value === ‘Bristol’) {
$w(‘#dataset1’).setFieldValue(‘address’, ‘14298 Lee Highway Bristol, VA 24202’);
} if ($w(‘#SelectCity’).value === ‘Verona’) {
$w(‘#dataset1’).setFieldValue(‘address’, ‘18 Gov Center Lane Verona, VA 24482’);
}
});

According to this logic, It should set the respective addresses when the city is selected. I’ve already created a field in the database called ‘address’. But it doesn’t seem to work.

I’m not sure if this will help anyone in the future, but I did figure out that you must also call the “save” function to get it to work properly.

$w(‘#dataset1’).onBeforeSave(() => {
if ($w(‘#SelectCity’).value === ‘Lynchburg’) {
$w(‘#dataset1’).setFieldValue(‘address’, ‘1007 Sheffield Drive Lynchburg, VA 24502’);
$w(“#dataset1”).save();
}
if ($w(‘#SelectCity’).value === ‘Bristol’) {
$w(‘#dataset1’).setFieldValue(‘address’, ‘14298 Lee Highway Bristol, VA 24202’);
$w(“#dataset1”).save();
}
if ($w(‘#SelectCity’).value === ‘Verona’) {
$w(‘#dataset1’).setFieldValue(‘address’, ‘18 Gov Center Lane Verona, VA 24482’);
$w(“#dataset1”).save();
}
});