sfraden Posted February 26, 2021 Report Share Posted February 26, 2021 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> Quote Link to comment Share on other sites More sharing options...
FaeFaeFae Posted March 5, 2021 Report Share Posted March 5, 2021 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. Quote Link to comment Share on other sites More sharing options...
sfraden Posted March 28, 2021 Author Report Share Posted March 28, 2021 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.... Quote Link to comment Share on other sites More sharing options...
sfraden Posted March 29, 2021 Author Report Share Posted March 29, 2021 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> Quote Link to comment Share on other sites More sharing options...
FaeFaeFae Posted March 30, 2021 Report Share Posted March 30, 2021 @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. Quote Link to comment Share on other sites More sharing options...
sfraden Posted March 31, 2021 Author Report Share Posted March 31, 2021 I wish I could do that, however the console is useless as when the error occurs, the submission page is set to auto submit, so it refreshes like 20 times a second... Quote Link to comment Share on other sites More sharing options...
BaySunshine Posted April 2, 2021 Report Share Posted April 2, 2021 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. Quote Link to comment Share on other sites More sharing options...
sfraden Posted April 6, 2021 Author Report Share Posted April 6, 2021 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...... Quote Link to comment Share on other sites More sharing options...
BaySunshine Posted April 7, 2021 Report Share Posted April 7, 2021 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, Quote Link to comment Share on other sites More sharing options...
TellMeWhy Posted April 8, 2021 Report Share Posted April 8, 2021 You may also check out this howto Article, it might help you in some way. https://howto.caspio.com/datapages/ajax-loading/ Quote Link to comment Share on other sites More sharing options...
sfraden Posted April 14, 2021 Author Report Share Posted April 14, 2021 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..... Quote Link to comment Share on other sites More sharing options...
TellMeWhy Posted April 14, 2021 Report Share Posted April 14, 2021 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.