top of page
Crumpled Fabric

1

Save Calculated Field Using WIX Data Hooks

Click URL to copy

Sep 21, 2021

1 min read

WIX Ideas

Tags: WIX Data Velo by WIX


Have you wanted to save data from a calculated field? Then you are in the right place.


Saving a solution or answer to WIX database collection is easier than expected. Using WIX Data Hooks, it is absolutely achievable to save a calculated field before it is inserted into the database.


WHAT I DID


The steps to achieving this procedure is quite basic. Assuming you already know the formula to use then the rest is merely assigning your answer to the answer field in your database. Before adding the code, I connected my form element(Date Picker) to data. WHY? The date picker value (Year selected) is what I used for the calculation. So it was important I took the value from there and substracted from the current year using new Date() object. I then added my data hook from by clicking on the three dot icon option for my database and clicked on Add/Remove Hooks.






Next I added the beforeInsert function and started modifying my code. My edited code looked something like that.




In order to calculate the date picker field, I decided to intercept the values before it was inserted and used it to calculate for the age. This date was then saved in a field in my database. Eventually, I will use this field in my website displaying it or using it for another operation it comes to that.


FINAL DISCUSSION


Well, that rounds up technically every step. My final code is below and you can copy and paste for personal use. I hope you enjoyed this tutorial, don't forget to LIKE/SUBSCRIBE






Code



Leave a comment (2)

Thanks for leaving a comment🎉

ahmed

ahmed

May 19, 2024

wix form data

Hi i need your help also in wix form submission

the problem is that some fields are generated when $w.onReady(function () { like day & date and when i choose the student name i get the phone number from database .

and the form save only the field u fill it by ur self like input or dropdown

Reply

Admin

Ghan

Ghan

Dec 29, 2022

Great Tutorial

Hi !

First of all, I want to thank you for your great tutorials, they helped us a lot with the coding.
But I still need your help, if you don't mind. I have based on Save Calculated Field using Wix Data Hooks Codes, to code my form . but the problem is I don't know why the code only reads the computation for "week2" computation and not the "weeks"

here is the code

I also did a separate column in the dataset where the chosen datas for both fields will be placed.

$w.onReady (() => {
$w("#Person").onChange(() => {
$w("#weeks").onChange(() => {
$w("#week2").onChange(() => {
let person = Number($w("#Person").value);
let weeks = Number($w("#weeks").value);
let week2 = Number($w("#week2").value);


$w("#totalLabel").show();

$w("#totalLabel").text = `"${String(weeks * Number(person))}페소"`; //DESIGN YOUR MESSAGE
$w("#totalLabel").text = `"${String(week2 * Number(person))}페소"`; //DESIGN YOUR MESSAGE

});
});
});

});

Reply

Walter Odibi

Walter Odibi

Admin

Hi there, thanks for your comment. I see why this is a problem. The onChange() function seems wrong in combination.

To combine onChangeFunctions use this method

$w("#Person, #weeks, #weeks2").onChange(() => {

//code here

});

RELATED TUTORIALS 🚀

Quote Calculator

Quote Calculator

Set up a quote calculator to enable your site visitors to easily manually build a quote for a service you offer.

bottom of page