B4J Question How to manipulate the values of an html form in a webview object?

CHAUVET

Member
Licensed User
Hello,

I have a web page with a form.

I load it in a webview object.

How can we change the value of a field ?

This is my HTML code of my form :
HTML:
<form action="action.php">
<input type="text" name="contact_name" value="" />
<input type="text" name="contact_fname" value="" />
<input type="text" name="contact_mail" value="" />
<input type="submit" value"GO" />
</form>
I load it in an webview objet (zoneweb is the name of my webview objet)

zoneweb.LoadUrl("http://www.mydomaine.fr/my-html-form.html")

I want to assign a value in the 3 fields : contact_name, contact_fname and contact_mail

Equivalent to (in JS) : document.form[0].getElementByName('contact_name').value = 'Alfred';

And after I would submit the form for the script action.php either call in webview.

Equivalent to (in JS) : document.form[0].submit();

Thank you for your help.
 

CHAUVET

Member
Licensed User
Hi Erel,

I have an error : netscape.javascript.JSException: TypeError: 'null' is not an object


B4X:
#Region  Project Attributes
    #MainFormWidth: 1200
    #MainFormHeight: 900
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Public zoneweb As WebView
    Public Button_go As Button
    Public bt_jscode As Button
    Public jscmd As TextField
    Public html_txtarea As TextArea

   ' Public joWV As JavaObject
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("window_1") 'Load the layout file.
    MainForm.Show

End Sub

Sub zoneweb_LocationChanged (Location As String)
  
End Sub
Sub zoneweb_PageFinished (Url As String)
    Button_go.Text = "URL loaded"

End Sub

Sub button_go_MouseClicked (EventData As MouseEvent)
    Button_go.Text = "Download in progress"
    zoneweb.LoadUrl("http://www.immostream.com/test/index.html")

End Sub
Sub bt_jscode_MouseClicked (EventData As MouseEvent)
    Try
        Dim joWV As JavaObject = zoneweb
        'joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(jscmd.Text))
        joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String("document.getElementById('contact_name').value = 'test'"))

      Catch
        html_txtarea.Text = LastException.Message
      End Try

End Sub

window_1.fxml

B4X:
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.web.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="764.0" prefWidth="1099.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <WebView id="zoneweb" layoutX="14.0" layoutY="14.0" prefHeight="577.0" prefWidth="572.0" />
    <Button id="button_go" fx:id="button_1" layoutX="300.0" layoutY="668.0" mnemonicParsing="false" text="Go" />
    <TextField id="jscmd" layoutX="586.0" layoutY="609.0" prefWidth="389.0" />
    <TextArea id="html_txtarea" layoutX="586.0" layoutY="14.0" prefHeight="577.0" prefWidth="493.0" wrapText="true" />
    <Button id="bt_jscode" layoutX="986.0" layoutY="611.0" mnemonicParsing="false" text="Inject JS code" />
  </children>
</AnchorPane>
index.html

HTML:
<!DOCTYPE html>
<html lang="fr">
    <head>
        <!meta charset="iso-8859-15" />
        <meta charset="utf8" />
        <title>TEST PROJET</title>
    </head>
    <body>

        <form action="action.php">
        Name : <input type="text" name="contact_name" value="" /><br />
        First name : <input type="text" name="contact_fname" value="" /><br />
        E-mail : <input type="text" name="contact_mail" value="" /><br />
        <input type="submit" value"GO" /><br />
        </form>

    </body>
</html>
 
Last edited:

CHAUVET

Member
Licensed User
Can you upload your project (File - Export as zip)?
Hi Erel,
Thank for your help...

Clic on "GO" button in first and wait button text become "URL loaded"
After you clic on "Inject JS code" button fot inject this commande line :

B4X:
joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String("document.getElementById('contact_name').value = 'test'"))
And an error is catched in html_txtarea.Text : netscape.javascript.JSException: TypeError: 'null' is not an object

Many thanks...

Projet erel.zip
 

Attachments

CHAUVET

Member
Licensed User
Hi,

Where to find documentation about JavaObject librarie ?
I am looking for the names of the different methods : RunMethodJO -> getEngine and other and also for RunMethod -> executeScript and other

I do not understand why : document.form [0] .submit () does not work.

B4X:
joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", _
        Array As String("document.form[0].submit()"))
Error : netscape.javascript.JSException: TypeError: 'undefined' is not an object
 

CHAUVET

Member
Licensed User
Haaaaarrrgggggggg !

I am very bad boy in Javascript ! It's Ok with good syntax like this :

B4X:
        joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", _
        Array As String("document.forms[0].submit();"))
 
Top