I have a .php page hosted on my website, and it’s programmed to find a given input key’s (which is passed through URL like ... .com?key=value) value in database.
If code found the value for the given key, it shows ‘success’, else it shows ‘incorrect value’.
I have a web-viewer setup for the user. Actually user is completing a process in the web-viewer, and as soon as the process completes, the webpage inside the webviewer create a record in my database. So if I check it after the user completes the process, the response would obviously be success.
I wanted the app to check if user has completed the process automatically, so I built this code :
What this code should do is, meanwhile the user completes the whole process in the webviewer, the API should continuously check the receipt page (by reloading again & again) if success response comes or not - till the response comes, continuously repeat the API task.
But, the app doesn’t seem to capture the response - what’s wrong with the above code?
Yep, I learned this one the hard way. With APIs, you have to check for an error and if there’s no error, check to make sure status is 200 and then add whatever blocks you need if the status is 200.
Also, is your API going to return the string “status” as a response when it succeeds? Because that’s what your blocks show.
I wonder the problem might be that the API is not reloading itself despite being in a loop
Yes. In simple words, this is the whole process :
Firstly, I open the webviewer with my base URL, with my custom key attached into the URL (like ... .com/sample.php?key=123456). The webpage opened in the webviewer has a Form in it, which the user has to fillup. Submitting the form, would create a record in my simple key-value DB. The key would be the one I passed in the web-viewer’s URL, and value would be from the Form.
As the user fills up the form, my app must continuously check if the value for the key has come in the database or not. If it came, means the user has filled up the form. If not, it means the user is still filling up the form.
The code must repeat continuously until it catches a success response.
The LOOP COUNT #n is the label68, and the 200: Incorrect … is the label69. Above these 2 labels is the webviewer.
Also, even after I complete the form in the webviewer, the label69 remains intact (still showing Incorrect ID …) & the counter also doesn’t stop. Instead the counter should stop (because the loop should end if success is the response) & the label69 must display Success…
That gif is really helpful. Your loop is working so it must be making repeated API calls. But you’re not getting a response with the word “status” in it. So there’s your problem.
You keep writing “success” but your block is checking for the word “status.”
At this stage that you are testing only, I suggest you have a button and on click have the blocks that inquire the database. So, complete whatever you need to do in the web viewer then click that button and see if you can read “status”. You are reading the database manually but at least you establish if you are reading the correct ID and if your webpage has saved the record.
If this worked but the loop still cannot read it then I suggest you look into the timing of your loop as most API will not allow such overload on the server.
Case #1 : When I click the label only when the whole process is done, it displays Success as the response.
Case #2 : When I click the label in-between the web-viewer process (just to check), it displays Incorrect ID as the response (as it should). But, when I click it again after the whole process is done, nothing happens.
It seems like API is not being called more than once.
If you are the creater of the html/php code page in the web viewer then just add the thunkable JavaScript to return a return code to the webviewer.response and from their call the API to inquire the database.
In an app for one of my clients, I used this method to know when to disable the web viewer window.