How to save calculated field in Wix collection Dataset

Hi Guys,

I have an issue with specific scenario and unable to solve it.
Please find the scenario below :

I have a travel website, in the tour details page I have a custom form for my user to fill. Based on the user’s preference, the final tour amount gets calculated. there are 3 variables to calculate the final amount :

  1. Base Amount = baseAmount

  2. Type of Meal = meal

  3. No. of Child = nrOfchild

The Calculation is happening in a text field = totalSumLabel

The calculation is happening perfectly, my problem is I cannot find a way to get this calculated amount in my database.

Now coming to the database side, in 1 dynamic page there are 2 collections attached.

1st is for the tour information like Tour name, Base Amount, Tour details etc.
2nd is for storing User’s Input

Calculation is happening based on the data of 1st Database and the final amount I want to store in the 2nd Database.

Sharing Screenshots below :

Databases :

Andaman Nicobor Island : Tour details dataset

User Data : Stores User input, want to store the Total Tour Amount in this database (Totalamount Field)

Step 1: Users choose their preference and the final amount is getting calculated perfectly.

Code of calculation :

$w . onReady ( function () {
});

export function dropdown1_change ( event ) {
let baseAmount = Number ( $w ( “#baseAmount” ). value );
$w ( “#totalSumLabel” ). text = ₹ ${( baseAmount ). toString ()};
}

export function nrOfPeople_click ( event ) {
let meal = Number ( $w ( “#meal” ). value );
let baseAmount = Number ( $w ( “#baseAmount” ). value );
let nrOfchild = Number ( $w ( “#nrOfchild” ). value );

$w ( “#totalSumLabel” ). text = ₹ ${(( baseAmount + nrOfchild )+ meal ). toString ()};

}

export function nrOfchild_change ( event ) {

let nrOfchild = Number ( $w ( “#nrOfchild” ). value );
let meal = Number ( $w ( “#meal” ). value );
let baseAmount = Number ( $w ( “#baseAmount” ). value );

$w ( “#totalSumLabel” ). text = ₹ ${(( baseAmount + nrOfchild )+ meal ). toString ()};

}

Step 2 : User clicks on the Get Quote Button and all the user inputs are getting stored in the user database.

Tried to connect totalSumLabel field with the database through connect to database option but still it is not capturing any data, result is blank.

Please help me to get the field value in the database.

Thanks
Ratnadeep
+917059104334

Where is your code for the saving-process???

For sure you have connected your SUBMIT-BUTTON in the Wix-Editor’s PROPERTY-PANEL and now you expect, that also the results of your calculation also will be saved automatically when pressing the SUBMISSION-BUTTON.

WRONG-THINKING!

But what you can do is → using a “onBeforeSave”-HOOK, to modify data, before the data gets saved.

https://www.wix.com/velo/reference/wix-dataset/dataset/onbeforesave

This hook was maden exactly for such situations, where you first are able to modify data, before it gets saved into your DB.

Without this hook, it just saves the data.

Another thing i see is, that you are using two different methods…

  1. export function nrOfPeople_click ( event ){ }
  2. export function nrOfchild_change ( event ){ }

What do you think, which one of these two is the right one?

In your case you are lucky that it works, because…

exportfunction nrOfPeople_click(event){
	letmeal=Number($w("#meal").value);
	letbaseAmount=Number($w("#baseAmount").value);
	letnrOfchild=Number($w("#nrOfchild").value);
}

…because you do not use the value of the dropdown itself.

Another note will be about the bad declaration names of your used elements.
Always identify/declare your elements with a PREFIX like…

dropNumberOfPeople
dropNumberOfChild

or
ddnNumberOfPeople
ddnNumberOfChild

drop or ddn as a prefix for the used element.

For buttons for example —> btn
For boxes ------------------------> box
…and so on…

Make your own code more readable, this will make your workings on codes much faster and more efficient and also will help extern coders (like for example me now) to read your code easely and much faster.

Time = Money → Money = Time !!!

Thanks for your response, I am a nontechnical guy, trying to learn codings. I will try your provided solution. Appreciate your input.

Hi, @tripera-worldwide I was wondering if you solved on how to save the total amount to your DB? I have the same problem with my site.