My dynamic page will load my data from a data set item (name : moviesdataset id:#moviesdataset)
and i have form that is common for all the dynamic pages.
Action item :
while submitting the form i want the title of that page to be also stored along with the form response.
i’m able to get the page title but unable to push it to the collection (name : moviereviews id:#testimonials02)
Please help solve it
My code
import wixData from 'wix-data';
$w.onReady(function () {
var movieTitle;
$w("#moviesdataset").onReady( () => {
let itemObj = $w("#moviesdataset").getCurrentItem();
console.log("The result if the current item:" +itemObj);
movieTitle = itemObj.title //get the relevant field key
console.log("The title is:" +movieTitle);
});
let data = {
"title" : movieTitle
};
console.log("data is :"+ data);
wixData.insert('moviereviews', data);
});
.insert() creates an entirely new item with an entirely separate _id. The needlessly complicated solution is to create an afterInsert hook on the collection your form is tied to (and probably less reliable).
What I would do is create a form and code it entirely as you are trying to do which inserts the title at the same time as the rest of the data. Trust me, interrupting HTML form behavior is just a pain.
Proper explanation? It’s quite literally just connecting all your form elements through code instead of using the form app. You’ve already used the insert() method correctly here, so the only thing would be to just use your form elements’ values to insert them as well: https://www.wix.com/corvid/reference/$w.Checkbox.html#value
import wixData from 'wix-data';
$w.onReady(function () {
var movieTitle;
$w("#moviesdataset").onReady( () => {
let itemObj = $w("#moviesdataset").getCurrentItem();
console.log("The result if the current item:" +itemObj);
movieTitle = itemObj.title //get the relevant field key
console.log("The title is:" +movieTitle);
});
let data = {"title" : movieTitle};
console.log("data is :"+ data);
wixData.insert('moviereviews', data);
});
…will be the wrong way.
Instead you will need to use → setFieldValue() / setFieldValues() + save() and perhaps an additional onAfterSave()-hook.
$w("#myDataset").setFieldValue("title", "New Title");
$w("#myDataset").save();