Default Form Submission Settings Overriding Code

I need a password protected page that will work with multiple passwords (essentially Dealer IDs). To do this, I created a data collection with a Dealer ID field. Then, I created a form with a Dealer ID field. I added code to the page that is supposed to users to the password protected page IF their dealer ID matches one of the dealer IDs in the data collection.

I have been told that the code is correct; however, it’s not working. When I go to the live site, the default form submission message appears.

Here my code (Note: I’ve xxxxx out the URL since this is for a client). Any help that you can provide would be greatly appreciated!!!

import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;

$w . onReady ( function () {
$w ( “#wixforms1” ). onWixFormSubmitted (() => {
const enteredPassword = $w ( “#input2” ). value ;
console . log ( “Entered Password:” , enteredPassword );
checkPasswordValidity ( enteredPassword );
});
});

function checkPasswordValidity ( enteredPassword ) {
console . log ( “Checking Password:” , enteredPassword );
wixData . query ( “DealerIDs” )
. eq ( “DealerID” , enteredPassword )
. find ()
. then ( results => {
console . log ( “Query Results:” , results );
if ( results . items . length > 0 ) {
wixLocation . to ( “URLxxxxxxxxxx” );
} else {
// Password is not valid
// Display an error message or perform any desired action
console . log ( “Invalid password” );
}
})
. catch ( error => {
console . log ( “Error:” , error );
});
}

Hi,

Did you check if the submission is successfully happening on the live site? You should see the following line in the console after submitting the Wix Form.

console.log("Entered Password:", enteredPassword);

onWixFormSubmitted( ) only occurs when a site visitor submits a Wix Form and it is successfully received by the server. It is possible the form is encountering a error on submission.

Thank you for your reply. I ended up getting it figured out after hours of research and trying different code. I’m not a true coder, so I’m sure that it took me much longer than the average person but I’m happy that I eventually got it working! Here’s the code I ended up with:

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady(function () {
$w(“#errorMessage”).hide();

$w(“#button21”).onClick(() => {
const enteredPassword = $w(“#input3”).value;
console.log(“Entered Password:”, enteredPassword);
checkPasswordValidity(enteredPassword);
});
});

function checkPasswordValidity(enteredPassword) {
console.log(“Checking Password:”, enteredPassword);
wixData.query(“DealerIDs”)
.eq(“dealerId”, enteredPassword)
.find()
.then(results => {
console.log(“Query Results:”, results);
if (results.items.length > 0) {
wixLocation.to (" https://URL “);
} else {
$w(”#errorMessage").show(); // Show the errorMessage element
}
})
.catch(error => {
console.log(“Error:”, error);
});
}