How to Insert the title of the dynamic page into the collection(database) along with form submission data.

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);
});

please some help me solve it at least tell me where i’m wrong.

.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.

any example ?

Any proper explanation ? Or please provide me some code snippet !

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

is it solved?? i am also facing similar issue here

Following this shown example here…

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();
$w("#myDataset").onAfterSave( () => {
  console.log("After save")
});

Using Wix-Data in your case will be the wrong way, since you are working on a DYNAMIC page, which is already connected with a (dynamic) DATASET!

Please also feel free to open your own post, witch your own good described issue, instead of bumping-up old posts.

If needed - - > you can simply link this post to your own for additional informations on your issue.