I want to check a webpage continuously till it shows 'success' - What's wrong with this code I built?

Hi!

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?

@actech any suggestions from you?

Any help would be appreciated! :grinning:

See text in [error] block

2 Likes

If error {
Label.text = error
} else if status==200 {
action
}

3 Likes

Yep, I learned this one the hard way. :wink: 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.

3 Likes

Thanks @actech & @tatiang!

I tried these blocks, and on every single loop count, the label’s text contains the code 200.

I wonder the problem might be that the API is not reloading itself despite being in a loop :man_shrugging:


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.

So, that’s it. I hope you understand :grinning:

So what is label69 showing as you preview your app and the repeat block fires over and over?

It’s continuously showing like this :

ezgif-6-186c79dcb02a

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…

Thanks!

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.”

Hi,

Actually this is the raw response I am getting :

{"status":"Transction Success","txnid":"142857","atomid":"XXXXXXXXX","txndate":"Fri Sep 25 09:26:35 IST 2020","fcode":"Ok","amount":"50.00","name":"Test Name","email":"test@test.com"}

What method should I use to detect the response is this?


Sorry, that’s just a word mistake. I just need to check the response contains something other than ‘incorrect’

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.

1 Like

Fine. So, I simplified the code, and here’s how it looks now :

What happens is,

  • 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.

1 Like

Great, at least you know where to investigate further. :clap:t2:

1 Like

Thanks, but how do I do that? I mean I don’t think there’s something fishy in this simple code? :

image

Would you help me fix things? Thanks!

1 Like

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.

1 Like

Yes, I am the creator of that file. But I’m not sure I got your idea clearly :neutral_face:

Can you please elaborate more? Thanks!

1 Like

This link explains how to Web Viewer Extensions

Also, check your API documentation to see if there is any timing restrictions such as minimum time between API calls.