Jump to content

How to detect if form submission FAILS....?


Recommended Posts

I have a specific form that creates a new entry in a table, and I need it to NOT create an entry if one already exists, instead just go to a diferent page altogether.  I can prevent the duplicate entry by making a field unique, the form submit will fail and no new entry will be made.  Problem I have is that the initial page that makes the table entry auto-submits when you open it (which is by design).  If it successfully completes the table entry, it then auto-loads the next datapage, however if it fails the submit it actually reloads the entire page which in turn tries to re-submit the document and gets caught in a loop of submitting failures forever.  What can I do to detect the failure and then re-direct to a different data page?  I tried the below code to attempt to trap it, but it does not work...

<script>

var v_elements = document.getElementsByTagName("p");

for (var i = 0; i < v_elements.length; i++) {

    if (v_elements.className == "cbFormError") {

        location. href = "https://c1acyXXX.caspio.com/dp/48c0900099f0e199ea0043c3XXXX";

    }
}

</script>

Link to comment
Share on other sites

Try this code instead:
 

<script type="text/javascript">
    if(document.querySelector('.cbFormCommonError')){
        window.location.href = "https://c1acyXXX.caspio.com/dp/48c0900099f0e199ea0043c3XXXX";
    }
</script>

 You need to add it to the footer of the Submission Form that gets auto-submitted.
 

Link to comment
Share on other sites

  • 4 weeks later...
On 3/5/2021 at 5:55 AM, FaeFaeFae said:

Try this code instead:
 


<script type="text/javascript">
    if(document.querySelector('.cbFormCommonError')){
        window.location.href = "https://c1acyXXX.caspio.com/dp/48c0900099f0e199ea0043c3XXXX";
    }
</script>

 You need to add it to the footer of the Submission Form that gets auto-submitted.
 

Tried this, unfortunately did not work, the datapage still loops infinately....

 

Link to comment
Share on other sites

Tried this method too, did not work either....

<script type="text/javascript">
document.addEventListener('FormSubmitted';, function(event) {
var content = document.body.textContent || document.body.innerText;
var hasText = content.indexOf("Values in one or more fields are invalid.")!==-1;
if (hasText) {
window.location.href = "https://c1acyXXX.caspio.com/dp/48c0900099f0e199ea0043XXX09";}
});

</script>

Link to comment
Share on other sites

@sfraden

Do you do this on preview or via a deploy URL?

That's really weird that it does not work for you. You can try the following to troubleshoot the situation.
1. Remove the auto-submission code and see if the redirection work when you do the manual submit. Before pressing Submit you could also open the Console in DevTools in your browser (F12 > Console) to see if there are any errors there (it could give us a hint what's going on)

2. Remove both auto-submit and redirection code, and inspect the page with the "Values in one or more fields are invalid" code. Try to enter the following in the console:

document.querySelector('.cbFormCommonError');

and see if you can find this element in the page.

3. Double-check that the code was added to the correct submission form.

Link to comment
Share on other sites

Hi @sfraden,

How about you add a  calculated value in your submission form and perform the validation before submitting the form? Add a virtual field to your form, change the form element to calculated value. Using CASE WHEN and SELECT query, validate to see if the record already exists in the table. If it does, then return 1, else return 0. In your JavaScript, verify the value of this virtual field. If the value returned is 0, then submit and the form. If the value returned is 1, then just redirect the form. The formula in the calculated value can be written as follows:

CASE WHEN (SELECT ID FROM TABLE_NAME WHERE FIELD1=TARGET.[@field:FIELD1] AND FIELD2=TARGET.[@field:FIELD2] AND.....)!=''

THEN 1

ELSE 0

END

 

Hope this helps.

Link to comment
Share on other sites

On 4/2/2021 at 5:36 PM, BaySunshine said:

Hi @sfraden,

How about you add a  calculated value in your submission form and perform the validation before submitting the form? Add a virtual field to your form, change the form element to calculated value. Using CASE WHEN and SELECT query, validate to see if the record already exists in the table. If it does, then return 1, else return 0. In your JavaScript, verify the value of this virtual field. If the value returned is 0, then submit and the form. If the value returned is 1, then just redirect the form. The formula in the calculated value can be written as follows:

CASE WHEN (SELECT ID FROM TABLE_NAME WHERE FIELD1=TARGET.[@field:FIELD1] AND FIELD2=TARGET.[@field:FIELD2] AND.....)!=''

THEN 1

ELSE 0

END

 

Hope this helps.

I actually tried to do your suggestion, and while I can obtain the BOOL value accurately, the problem is there is a delay when processing the calculated field after page load.  The javascript to check the virtual field fires off BEFORE the calculated Value is populated, so it always comes back FALSE......

 

 

Link to comment
Share on other sites

On 4/7/2021 at 3:58 PM, BaySunshine said:

Hi @sfraden,

Please try adding your code within the BeforeFormSubmit event function. This should ensure to perform the validation after all the fields are populated.

<script type="text/javascript"> document.addEventListener('BeforeFormSubmit', function(event) {

// do something

});

</script>

Regards,

Tried this: Did not work.  Apparently when you auto-submit a page it does not trigger the event to run, only if you manually hit the submit button.....

Link to comment
Share on other sites

4 hours ago, sfraden said:

Tried this: Did not work.  Apparently when you auto-submit a page it does not trigger the event to run, only if you manually hit the submit button.....

This worked for me

 

<script type="text/javascript">
  if(document.getElementById("caspioform")) {
	document.getElementById("caspioform").style.display = 'none';
    
    
  if(document.querySelector('.cbFormCommonError')){
        window.location.href = "https://www.google.com";
    }

  else
	setTimeout('document.forms["caspioform"].submit()',1000); 

} 
</script>



if it errors (because of duplicated value), it goes to google, else, it will auto submit.

For the first loading, since it doesn't have error, yet, it should auto submit.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...