Onsubmit Popup Catalog Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 12:21 PM
I created an onSubmit script, but I'm running into a problem. When the user clicks "I agree," it should submit the form. However, even though the script sets the value to “true,” g_form.onsubmit is not actually triggering, and the form remains on the same page instead of submitting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 02:51 PM
Your issue is that g_form.submit() will cause your onSubmit handler to get triggered again, causing your submission to get blocked again and the popup to appear again. You need to set some sort of persistant indicator that can exist across multiple calls to your onSubmit handler which indicates to your script if the user has agreed to the modal.
One way to do that is with scratchpad:
function onSubmit() {
// Define the agreement HTML
// check to see if agreedToTerms is truthy, if it is, allow regular submit
if (g_scratchpad.agreedToTerms) {
return true;
}
var agreementHTML = ...
// Display the agreement in a modal
spModal.open(...).then(function(answer) {
if (answer.label === "✔ Agree and Submit") {
console.log("User agreed to the terms.");
// Set the 'u_agreed_to_terms' field to true
g_form.setValue('u_agreed_to_terms', true);
// Set flag to indicate to onSubmit that terms have been agreed to
g_scratchpad.agreedToTerms = true;
// Programmatically trigger form submission
g_form.submit();
} else {
...
}
});
console.log("Modal displayed. Waiting for user action.");
return false; // Explicitly block submission
}
You could also do this without g_sratchpad if you were to instead use g_form.getBooleanValue("u_agreed_to_terms") in the top if-statement, but if this field is visible and editable to the end user then they can change it without reading the popup, so that's when g_scratchpad approach above might be better.