I have a CMS form on a dynamic page. When users submit the form, I cannot tell which page they filled it on. I want to write a code that would help me automatically fill out a field in the CMS forms collection from the dynamic page CMS collection, so that I can see which dynamic page the user submitted a response to.
I solved it! Wix chatbot helped me with the code and I’m sharing it here:
import wixLocation from 'wix-location';
$w.onReady(function () {
// Replace 'dynamicDataset' with the actual ID of your dynamic page's dataset
// Replace 'formDataset' with the actual ID of your form's dataset
// Replace 'dynamicField' with the field key in the dynamic page's dataset
// Replace 'formField' with the field key in the form's dataset
const dynamicDataset = $w("#dynamicDataset");
const formDataset = $w("#formDataset");
formDataset.onBeforeSave(() => {
// Get the value from the dynamic page's dataset
const dynamicFieldValue = dynamicDataset.getCurrentItem().dynamicField;
// Set the value in the form's dataset
formDataset.setFieldValue('formField', dynamicFieldValue);
return true; // Indicate that the save operation should proceed
});
});