Clicking Button in HTML

tonycmac

Member
Licensed User
Longtime User
I have an basic HTML page that has one editable text field and one "Search" button. Test Page

In B4A, I have a layout that has one EditText field and one Button. I'd like to be able to enter something in the EditText field and then, when clicking on the button, it would launch a WebView that copies the EditText contents to the HTML editable field and then clicks on "Search" in the html and displays the corresponding result.

I've done this in VB before but not sure if this is do-able in B4A.

Here's what I got so far:

B4X:
Sub Process_Globals
    Dim hc As HttpClient
End Sub

Sub Globals
    Dim Button1 As Button
      Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout(1)
      
      If FirstTime Then
        hc.Initialize("hc")
    End If
'      
'    Dim req As HttpRequest
'    req.InitializeGet("http://dl.dropbox.com/u/14738173/test.html")
'    hc.Execute(req, 1)
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim resultString As String
    resultString = Response.GetString("UTF8")
    'Work with the result
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log("Error connecting: " & Reason & " " & StatusCode)
    If Response <> Null Then
        Log(Response.GetString("UTF8"))
        Response.Release
    End If
End Sub 

Sub Button1_Click
     Dim req As HttpRequest
      
      If EditText1.Text<>"" Then
         req.InitializeGet("http://dl.dropbox.com/u/14738173/test.html")
       hc.Execute(req, 1)
      Else
        Msgbox("Enter something in editbox","")
      End If
End Sub

In the HTML, the 2 fields i'd like to interact with are:

Editable textbox
HTML:
<input id="TestNo" name="TestNo" value="">

Search Button:
HTML:
<input type="submit" value="Search" name="submit_button">
 

Attachments

  • HTMLsearch.zip
    6.3 KB · Views: 236

warwound

Expert
Licensed User
Longtime User
Here you go:

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim Button1 As Button
   Dim EditText1 As EditText
   Dim WebView1 As WebView
   Dim WebViewExtras1 As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout(1)
   WebView1.LoadUrl("file:///android_asset/search.htm")
End Sub

Sub Button1_Click
   If EditText1.Text<>"" Then
      Dim Javascript As String
      Javascript="document.forms.searchForm.q.value='"&EditText1.Text&"';document.forms.searchForm.submit()"
      WebViewExtras1.executeJavascript(WebView1, Javascript)
      Log("Executed javascript: "&Javascript)
   End If
End Sub

And the search.htm page contains:

PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Search example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div>
   <form method="GET" action="http://www.google.co.uk/search" id="searchForm">
      <input type="submit" value="" style="background-image: url(google.gif); border:none; width:48px; height:48px; cursor:pointer" />
      <br>
      <input type="text" name="q" maxlength="255" value="" style="width:256px">
      <br>
      <input type="hidden" name="ie" value="UTF-8">
      <input type="hidden" name="oe" value="UTF-8">
   </form>
</div>
</body>
</html>

By creating a 'proper' HTML form you can use standard javascript techniques to access form elements and set their values and then call the form's submit() method:

Executed javascript: document.forms.searchForm.q.value='basic4android';document.forms.searchForm.submit()

You can find WebViewExtras here: http://www.b4x.com/forum/additional...al-updates/12453-webviewextras.html#post70053

Martin.
 

Attachments

  • HTMLSearch.zip
    9.4 KB · Views: 299
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…