Android Question Save contents of local (offline) html form to a local sqllite table on the device

GeoffT660

Active Member
Licensed User
Longtime User
Is it possible to save the contents of an offline html form displayed in WebView to a local sqlLite table on submit? If so, what is the best approach? Thanks.
 

drgottjr

Expert
Licensed User
Longtime User
assuming you have a file (text/html) with some input fields, a submit
button and some javascript to capture the values of those fields,
then there is nothing stopping you from sending those values back
to a b4a app which can store them in an sqlite db. which of those
operations can you not handle at this time?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
and now that i think of it, if all this is local, why do you even need a webview? a panel with a couple edittext views and a bottom at the bottom, and you're good to go. in any case, if webview it must be, webview it will be. let us know what you know how to do and what you don't know so we can fill in the spaces.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
Can you also include the source code of such offline html page in the response of drgottjr request?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
@MicroDrie, is there something you can show @GeoffT660 if he/she shows you how to write the html page? that'll be 2 fewer things for me to do
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
To be able to give advice, we do need a little more background information. Off-line HTML page can be obtained in many ways and can also be used for all kinds of purposes.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
@drgottjr saidly as long as @GeoffT660 doesn't tell us what he wants to do, we can't help him with the best approach based on the complexity of the given HTML source(s).
 
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
I was hoping to be able to save the contents of the fields/list/check box of a html form to sqllite db much like I would submit a form and post to MS SQL Server in a web app but all locally on the device. I have no problem creating the web form and displaying in a webview with a submit button but am unsure how to capture the values to sqllite on submit. I have plenty of forms with edittext, spinners, etc. that save to the sqllite db but am trying to create a more dynamic questionnaire where I add or remove questions without having to modify the app.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so, i assume you mean you know how to capture the values of the various input fields in your form via a javascript function triggered by the submit button and show those values on screen with an alert (or sent to console.log. either). yes? if not, stop. don't read the rest until you confirm.

conceptually, what you'll do is send the captured values to your b4a app instead of to the alert or the log. with me? for that, you need to include the webvewextras library from add'l libraries. v1.42 is fine. you don't need webviewextras2. here's the link for general information about this library:
https://www.b4x.com/android/help/webviewextras.html

so, in your b4a app declare your webviewextras:
B4X:
    ' i assume you already have delared your webview
    ' eg, dim webview1 as webview

    Dim wvx As WebViewExtras


in your activity_create, initialize the wvx:
B4X:
    wvx.addJavascriptInterface(webview1, "B4A")
    wvx.addWebChromeClient( webview1,"WEevent" )

the webviewextras help page will explain what those methods do.

now create this sub in your b4a module:
B4X:
Sub fromWebview( s As String )
   log("Got: " & s & " from my webview")
End Sub
this sub will receive the abovementioned captured values from your html page. forget about sqlite for the moment. let's see if you can read the values from the webview.

------------------------------------------------------------------------------------------------------------------------------------------------------
in your html doc, add this call to the function where you process the submit button and capture your values. for the purposes of the exercise, just gather them into a string which we'll call stringwithvalues:
var stringwithvalues = "name=" + name + "age=" + age. (substitute your variable names and values as appropriate)

B4A.CallSub ('fromWebview',true,stringwithvalues);

-------------------------------------------------------------------------------------------------------------------------------------------------------
run your app. if you can see your captured values in the b4a log, we can proceed.
 
Upvote 0

GeoffT660

Active Member
Licensed User
Longtime User
No I don't how to capture the values of the various input fields in your form via a javascript function triggered by the submit button and show those values on screen with an alert (or sent to console.log. either). Please help with that part also.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so, when you say
I have no problem creating the web form and displaying in a webview with a submit button but am unsure how to capture the values to sqllite on submit.
, are you saying sqlite is the problem? or capturing the values is the problem? (if you know how to create the form, you must have read the part about how the fields are sent to a server, no?) teaching you javascript is wandering a little off point in an android/b4a forum. i'm not saying nobody will help, but it would be good if you brought something to the table. (like how to process a web form.)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
a few more comment, if you don't mind.

first, the reference to jsoup in one of posts in this thread
might be of interest to you. i've never used it, but i can
see how it might possibly work. i don't think it's designed
for local use though, but i only glanced at the link briefly.

in reading back through the thread, i gather you want
a database server listening to/for the data you post via
a webpage form. you thought b4a would act as this
database server with sqlite. right?

i'm pretty sure there is no such creature for b4a. others
wiser may have a different insight. there is a b4j server
that a number of people seem to use. but i do not believe
it is a database server. at least not for sqlite. it's also not
b4a. and i'm making a distinction between a normal web server
with a connection to a database, and a database server. it's
a big distinction.

that failing, there are 3 ways (that i can think of) to share
input data between a webview and its b4a app:

the first one is to use the javascript interface between a
webview and a b4a app. this is the approach i've been talking
about. for this you need to know javascript. you don't need to
know much. plenty of tutorials regarding html forms and
javascript out there.

the second requires webviewextras (as does the first), but
instead of using that library to enable the webpage to
pass things back to the app, it allows the app to extract
things directly from the webpage. it does not completely
absolve you from having to learn a little javascript. (without
the javascript the b4a app has no way of knowing when the
form has been filled out). but it does presuppose an understanding
on your part of the DOM. you need to know how to find the
form's input fields on the page and suck out their values. you
may find this more annoying than learning a few lines of javascript.

the third method would be to run an http server as your b4a
app. the webview would present the fill-in form as usual and
the submit button would contact the local server (your b4a
app). i'm not sure you would find this approach any easier
than learning a few lines of javascript, since you would have
to know how a web server responds to contact from a
webview. one it had received the post from your webpage,
your little http server would then do the database updates.

dealing with sqlite would be the same in all cases.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
i don't think it's designed
for local use though, but i only glanced at the link briefly.
if you have the html code, then you can use it offline, but maybe you can use it offline too to get the html code, if you have the local link.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
yeah, once he has the html text in hand, jsoup does the rest. the trick (for him) is getting the html text. that means the html has to be downloaded from somewhere (which, i think, is the purpose of jsoup) or accessed (somehow) locally. to my mind, that brings us back to webviewextras accessing a webview via the javascript interface.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…