OK, since it is already tied to a dataset, we are going to use that possibility. I assume you got as far as being able to save an entry to the collection. We forget about the query (for now) and we do this:
- if you have your Submit-button set up as a dataset-submit button (you chose a Submit button when designing the form) we are going to detach it from the dataset (click its Setings and bind it to nothing).
If that does not work, delete the button and add a new , standard, unbound one. Here, we will call it #button1. In the properties panel, add a handler for the onClick event. Wix will than insert an empty piece of code like below and we will add some code (explained later):
export function button1_click ($w, event) {
objSaved = $w(“#myDataset”).save()
.then( (item) => { let strPrimaryKey= item._id; } )
.catch( (err) => { let errMsg = err; } );
}
- in the datasets onBeforeSave (called before something is saved) we are going to do the whole calculation , something like this:
$w(“#myDataset”).onBeforeSave( () => {
fnCalculateIndices();
} );
- in the function fnCalculateIndices() we do something like this:
export function fnCalculateIndices() {
let numAge = $w(“#inputfieldage”).value;
etc
// And do your magical calculations here.(So we replace the items[0] with input from the form)
}
- and we are going to write the results to the row like this
export function fnCalculateIndices() {
let numAge = $w(“#inputfieldage”).value;
etc
$w(“#myDataset”).setFieldValue(“age”, numAge);
// assuming that the field in the collection is called “age” and we fill it with the value of the var numAge. I put //num in front of the var, so the code reads better and you can distinguish visulally between a db-field and a //var in your code
etc (like BMI, etc)
}
5) now we have done the calculations, these values have been written to the dataset and finally, it will be saved because all was triggered from the .save in the first place.
-
now we need to show the results on a second page. We are going to use a little trick: we are going to define it as a Dynamic Page. So, we add a new page called “Results” (or whatever) and define it as a Dynamic Page. Wix will ask you a url, and you define the URL as www…/{Id}
You attach the same collection to the dataset as we just saved.
On this page, you just put in all the text boxes (the results) you want (text, not user input) and you bind them to the result fields in the collection using the GUI.
Why do we use a Dynamic Page? Well, when you do, Wix already retrieves the item for you (like we did a couple of days ago with the query) and makes it available programatically and ready to use for interface elements. So we no longer need that query.
-
now we need one more thing: to get from the input page to this one. Well, we already had this code in the button1.onClick:
export function button1_click ($w, event) {
objSaved = $w(“#myDataset”).save()
.then( (item) => { let strPrimaryKey= item._id; } )
.catch( (err) => { let errMsg = err; } );
}
What we did here was saving the dataset. When you do, Wix returns the whole saved dataset. We catch this and attach it to the var objSaved. Now that one holds all the saved fields. We want to extract the Primary Key from it (that one Wix added while saving, it´s a long string like “0c45fg28…”), which we did with “let strPrimaryKey= item._id;”
Now we use wix-location to go to this Dynamic Page. Remember that at the top of you page code, you should import wix-location like this:
import wixLocation from ‘wix-location’;
and, to put it all together, we add in this function a last line:
wixLocation.to(“/results/”) + strPrimaryKey;
Note that we called the page Results and here we write results. To find the real name of the page, click the page properties and go to SEO. There you will find the real page name for referring to it in code.
- think about what you want people to do on that result page. Maybe put a button in there with a caal to action to another page, maybe go back to the home page.
This should be it. I have NOT tested this code, but apart from some glitches, this should work.
Hope this helps.