Reading CheckBox Value from HTML Table

ranba790

Member
Licensed User
Longtime User
Hi
I am using the webview.loadhtml to display Html Table.
The first column has a check box in each row( "<td><input type=checkbox></td>")
How can i read the value of the check box if the user has check it or not
thanks
 

ranba790

Member
Licensed User
Longtime User
override url will not work

the is a sample of the the html code which is a list of check boxes
<form name="orderform">
What kind/s of music do you listen to?<br>
<input type="checkbox" name="music" value="Rock" checked="checked">
Rock<br>
<input type="checkbox" name="music" value="Reggae">
Reggae<br>
<input type="checkbox" name="music" value="Pop">
Pop<br>
<input type="checkbox" name="music" value="Rap">
Rap<br>
<input type="checkbox" name="music" value="Metal">
Metal<br>
<input type="submit" onclick="get_check_value()">
</form>

this is a sample of the java script to process the selection after the submit

<script type="text/javascript">
<!--

function get_check_value()
{
var c_value = "";
for (var i=0; i < document.orderform.music.length; i++)
{
if (document.orderform.music.checked)
{
c_value = c_value + document.orderform.music.value + "\n";
}
}
}

//-->
</script>

the questions is how can i get the data in B4A. either get the c_value or be able to access the document object from B4A
thanks
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

This is a simple task with JSInterface.

First thing is to add the B4A javascript interface to the WebView that's displaying the form (let's assume it's named WebView1):

B4X:
Sub Globals
   Dim myInterface As JSInterface
   Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   myInterface.addJSInterface(WebView1, "B4A")
   
End Sub

Now you need a B4A Sub that your web page can call to pass the data to:

B4X:
Sub processFormData(data As String)
   Log("data is '"&data&"'"
End Sub

Now update your javascript function to call the B4A Sub processFormData, passing your c_value String:

B4X:
function get_check_value() {
   var c_value = "";
   for (var i = 0; i < document.orderform.music.length; i++) {
      if (document.orderform.music[i].checked) {
         c_value = c_value + document.orderform.music[i].value + "\n";
      }
   }
   B4A.CallSub("processFormData", c_value);
}

If you need to cause get_check_value() to be executed from B4A then you can do this in B4A:

B4X:
WebView1.execJS("get_check_value()")

As long as get_check_value() is within global scope of the page then it will now be executed.

Martin.
 
Upvote 0

ranba790

Member
Licensed User
Longtime User
script problem

Hi
thanks for the info, didn't get to try it yet since i having trouble with the script.

<html>
<head>
<script Type="text/javascript">
function get_check_value() {
var c_value ="" ;
For (var i = 0; i < document.gridData.SR.length; i++) {
If (document.gridData.SR.checked) {
c_value = c_value + document.gridData.SR.value + " , " ;
}
}
alert(c_value )
B4A.CallSub(processFormData, c_value);
}
</script>
</head>
<body>
<FORM NAME=gridData>
<caption>TestHeader</caption>
<table border=1>
<tr>
<th bgcolor=red></th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
</tr>
<tr>
<td><input type=checkbox name=SR></td>
<td>R0 C0</td>
<td>R0 C1</td>
<td>R0 C2</td>
</tr>
</tr>
<tr>
<td><input type=checkbox name=SR></td>
<td>R1 C0</td>
<td>R1 C1</td>
<td>R1 C2</td>
</tr>
</tr>
<tr>
<td><input type=checkbox name=SR></td>
<td>R2 C0</td>
<td>R2 C1</td>
<td>R2 C2</td>
</tr>
</table>
<INPUT Type="submit" VALUE="DeleteRowes"onClick= "get_check_value()">
</form>
</body>
</html>



for some reason when i press the button i am getting get_check_value is undefined.
any body has an idea what am i doing wrong?

can you recommend a good free tool for development a simple html page with java script debugging.
thanks.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

Update your B4A code to add the console interface to the WebView:

B4X:
Sub Globals
   Dim myInterface As JSInterface
   Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   myInterface.addJSInterface(WebView1, "B4A")
   myInterface.addConsoleInterface(WebView1)
   
End Sub

That might help debugging the error - watch the B4A log for anything relevant.

I'd guess that get_check_value() is not defined in the javascript global scope, so to the calling code it is indeed 'undefined'.

Martin.
 
Upvote 0

ranba790

Member
Licensed User
Longtime User
Thank you for all your help. this works perfectly now.
the problem was in the script. Java Script is case sensitive and in the script i used For instead of for and If instead of if.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I've seen that behavior before when constructing an HTML string in B4A then using the WebView loadHtml method...

The B4A compiler seems to change the capitilisation within the string if it detects what it thinks is a B4A keyword.

Anyway glad JSInterface works for you.

Martin.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The B4A compiler seems to change the capitilisation within the string if it detects what it thinks is a B4A keyword.
Only if you type the characters first without a preceding quote and then move off the line in my experience. If you start typing a quoted string the IDE is smart enough to not check after a quote for keywords.
 
Upvote 0
Top