Android Question Setting text fields on webpage - solved needed v1.42 of WebViewExtras

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Does anyone have an example of setting text fields on a webpage?

I want to go to a webpage and then populate the fields (it's a form webpage) from data I have saved.

I've read a lot of posts about reading them by haven't seen an example of setting them

I will know the field name on the page so this shouldn't be too hard but just unable to locate an example.

Once I populate the fields I have the user will fill in the rest of the fields (because specific to them) and send in the form

Any help or example would be nice

Thanks Bobval
 

ronell

Well-Known Member
Licensed User
Longtime User
you have access on the webpage? if yes then you can try this
B4X:
webviewextras1.LoadUrl("http://www.yourdomain.com/page.php?fname=Ronell")

PHP:
<?php

$fname = $_GET['fname'];

?>


<input type="text" name="fname" value="<?php echo $fname; ?>"/>
 
Upvote 0

rosippc64a

Active Member
Licensed User
Longtime User
The other side:

B4X:
   WebView1.LoadUrl(url)
...
Sub WebView1_PageFinished (purl As String)
...
        Dim Javascript As StringBuilder
        Javascript.Initialize
        Javascript.Append("document.getElementById('dbperpage').value='" & dbperpage & "';")
        Javascript.Append("document.getElementById('page_select').value='" & page_select & "';")
        Javascript.Append("document.getElementById('filter_order').value='timedown';")
        If search_word.Length>0 Then
            Javascript.Append("document.getElementById('search_word').value='" & search_word.trim & "';")
            Javascript.Append("document.getElementById('search_type').value='" & search_type & "';")
        End If
        Javascript.Append("get_list();")
        WebViewExtras1.executeJavascript(WebView1, Javascript.ToString)
        'wait for execution of javascript
        Sleep(1500)
...
End Sub
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I'm sure these probably work, but sorry to say they are as clear as mud to me.

Ronell
Probably haven't touch php line of code in since the early 90's I'm just guessing
I can't upload anything to the website (like a php file) I don't have control of that. Just the ability to normally type in the information

Rosippc64a
What is dbperpage, page_select and filter_order

I am assuming search_word is my field name in this case "field-104" and search_type is that the field type "text"
I don't see how I output my text to the field

Appreciate the attempts at helping. Will keep searching
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
I can't upload anything to the website (like a php file) I don't have control of that.
oh i see.. then my sample code will not work at all

lets simplify @rosippc64a code


B4X:
Sub WebView1_PageFinished (purl As String)
...
        Dim Javascript As StringBuilder
        Javascript.Initialize
   
        Javascript.Append("document.getElementsByName('field-104')[0].value='" & search.text & "';") 'get the html element using field-name 'field-104' then set the value using your search textbox
           
        WebViewExtras1.executeJavascript(WebView1, Javascript.ToString)
        'wait for execution of javascript
        Sleep(1500)
...
End Sub
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I was trying this:
mWebViewExtras.executeJavascript(sNYEI_FormsWebsite_Webview, "document.forms[0].elements[""field-104""].value = 'Some Value';")

Which does the SAME THING your code does.

Seems to CLEAR the screen and write out "Some Value"

Not what I excepted. I wanted the field-104 to show the value "Some Value", like the user typed it
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Right now I am trying to fill in the FOB# on this Screen shot

Screenshot_2019-10-09-17-54-03.png

What happens is this:

Screenshot_2019-10-09-17-54-16.png
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
try changing the array value

document.forms[1].elements[""field-104""].value = 'Some Value';"

or

document.forms[2].elements[""field-104""].value = 'Some Value';"

and so on.. then see the result
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I must be missing something in the source

I viewed it and saved it as text. Maybe another pair of eyes
 

Attachments

  • webpage.txt
    47.8 KB · Views: 183
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Tried this way of doing it:
mWebViewExtras.executeJavascript(sNYEI_FormsWebsite_Webview, "document.querySelector(""input[name=field-104]"").value = ""Some Value"";")

Same result
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
im also confused why the whole html body is being replaced, il update if i see a possible solution
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Can use send keys to set the field. But that surely isn't the way to go.

Wonder if the field only having a Name but not an ID is the problem - the trying continues...
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
works here, no problem.

Dim Javascript As String = "document.forms['fobform']['name'].value='" & "robert" & "';"

webviewextras.executeJavascript(webview1, Javascript)
Sleep(5000)
webviewextras.executeJavascript(webview1, "alert(document.forms['fobform']['name'].value);" )


i put a little page on my server, had a test b4a app load it. wait a few seconds until the dust settles and run the first executeJavascript call.
wait a couple seconds, and you see "robert" magically appear in the name field.
user taps on the submit button, and you see an alert sent by the server indicating that "name=robert" was successfully passed to it.


the first thing that jumped out about the suggestions above was all the quotation marks. unless you're using one of erel's magic smart strings, you can't normally have double quotes as was shown further up this thread. at least for javascript, it's double quotes inside single quotes or vice versa. look closely at how the quotes are handled. note: aside from how the quotes are handled, there are a number of ways to format the javascript command. how i formatted the command was just easier for me to follow.

-go
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Same out come. Clears the webview page and writes Robert top left corner

All the double quotes you are seeing become single quotes when compiled.
You are using single quotes I was using double quotes to surround the text.

Either case I tried both ways. Double and Single quotes.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
what is the initial url? i'm curious to try to stuff the fob box with a value myself with the actual page, not a test (which i know works). i don't know what you mean by all the double quotes becoming single quotes when compiled. let's say it's irrelevant for now.

technically, i could serve your webpage.txt from my server to see how things play out. i would have to modify the form's action to point back to me. but even if that works, there are still a number (unknown) unknowns which might preclude the thing's working with the real server and what it's expecting.

you could also upload your app as it stands now. i'd like to see how you get all the quotes to change themselves. a handy trick.

-go
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
looks good to me.
 

Attachments

  • robert.png
    robert.png
    18.8 KB · Views: 172
Upvote 0
Top