How to write and retrieve data over web for a newbie?

Just starting out? Need help? Post your questions and find answers here.
Giansauna
Posts: 5
Joined: Tue Nov 29, 2016 9:39 pm

How to write and retrieve data over web for a newbie?

Post by Giansauna »

First: I'm new to SpiderBasic.
I have installed a simple php-file on my webserver. With that, it is possible to make entries into the mysql database on the server together by passing parameters in the url : http://xyz.com/insert_data.php?id=123&name=Blahblah
I thought of using the HttpRequest() procedure to enter data from within my SB application, but the following code is giving me an error and no entry (adapted from the SB manual):

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData)
    If Success
      Debug Result$
    Else
      Debug "HTTPRequest(): Error"
    EndIf
  EndProcedure
  HTTPRequest(0,"http://xyz.com/insert_data.php?id=123&name=Blahblah","",@HttpGetEvent())
I have a second php-file which retrieves the entered data into a html table. I'd like to use it within a WebGadget() in my SB application, but I cannot refresh the content. SetGadgetState() with the state #PB_Web_Refresh does nothing. I always have to delete the cache of my browser and restart my SB application to have a refreshed content.
How can I save and retrieve data into a web database with SpiderBasic, then?

// Moved from "Tricks 'n' Tips" to "Coding Questions" (Peter)
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How to write and retrieve data over web for a newbie?

Post by Peter »

Write Data:

I suspect that you are getting a CORS error. Open the developer console of your browser (usually with <F12>). If you see this error message:
Access to XMLHttpRequest at [...] from origin [...] has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
then you need to add the following to your PHP script:

Code: Select all

<?php
  header("Access-Control-Allow-Origin: *");
Retrieve Data:

If it is a cache problem, try using SetGadgetText() to reset the address to your second PHP-File. Append an always changing dummy parameter to this address to bypass the cache:

Code: Select all

SetGadgetText(YourWebGadget, YourWebAddress + "&rnd=" + ElapsedMilliseconds())
Giansauna
Posts: 5
Joined: Tue Nov 29, 2016 9:39 pm

Re: How to write and retrieve data over web for a newbie?

Post by Giansauna »

Great, Peter! Both of your answers resolved my problems. Thank you very much! :D
Post Reply