I have a confirmation page after booking where customers land on my website where I want to automatically submit a form to capture some information to help track. The code is below and all I want to do is auto submit this form. I am not displaying the form or a button to the end users. How best can I do this?
import wixLocationFrontend from ‘wix-location-frontend’;
$w.onReady(function () {
const query = wixLocationFrontend.query;
// Use direct element ID references to set values
if (query.utm_campaign) {
$w('#cf_utm_campaign').value = decodeURIComponent(query.utm_campaign);
}
if (query.utm_content) {
$w('#cf_utm_content').value = decodeURIComponent(query.utm_content);
}
if (query.utm_medium) {
$w('#cf_utm_medium').value = decodeURIComponent(query.utm_medium);
}
if (query.utm_source) {
$w('#cf_utm_source').value = decodeURIComponent(query.utm_source);
}
if (query.utm_term) {
$w('#cf_utm_term').value = decodeURIComponent(query.utm_term);
}
// Debugging: Check that the values are set
console.log("Campaign:", $w('#cf_utm_campaign').value);
console.log("Content:", $w('#cf_utm_content').value);
console.log("Medium:", $w('#cf_utm_medium').value);
console.log("Source:", $w('#cf_utm_source').value);
console.log("Term:", $w('#cf_utm_term').value);
});
I tried this and it looks good however, when I try it in production I get this below. Not sure why because if I manually just submit from the UI it works fine with the pre-fed data.
code: ‘VALIDATION_FAILED’, message: “Validation failed: submission doesn’t satisfy form requirements.”
// Velo API Reference: Velo Docs
import wixLocationFrontend from ‘wix-location-frontend’;
$w.onReady(function () {
const query = wixLocationFrontend.query;
//const myFormValues = $w("#form").getFieldValues();
//console.log("Values:", myFormValues);
$w("#form").setFieldValues({
cf_utm_campaign: decodeURIComponent(query.utm_campaign),
cf_utm_content: decodeURIComponent(query.utm_content),
cf_utm_medium: decodeURIComponent(query.utm_medium),
cf_utm_source: decodeURIComponent(query.utm_source),
});
// Debugging: Check that the values are set
console.log("Campaign:", decodeURIComponent(query.utm_campaign));
console.log("Content:", decodeURIComponent(query.utm_content));
console.log("Medium:", decodeURIComponent(query.utm_medium));
console.log("Source:", decodeURIComponent(query.utm_source));
$w("#form").submit();
});
It seems you are running into validation issues on the Wix Form. This usually occurs when there is a required field that is missing or a input data type mismatch.
Is it possible something is set as a required format or field on the Wix Form but is missing in your submit() method?
I noticed query.utm_term
is missing when you used the setFieldValues() method.
You can also alternatively submit the form in the backend using the createSubmission() method.