Jose's Read Only Forum 2023

Archive => Archived Posts => Topic started by: Eros Olmi on February 21, 2009, 12:05:45 AM

Title: Getting data from web GUI elements other than textbox
Post by: Eros Olmi on February 21, 2009, 12:05:45 AM
José,

I was (again) experimenting with some of the many examples you posted.
In particular I was trying to get/set data from html elements ID like in your example: http://www.jose.it-berater.org/smfforum/index.php?topic=2709.0

I have difficulties getting data from "Select" and Input Type="radio" elements like in the following example.
Getting data from "shirt-size" radio button returns just the "S" size while getting data from "Select" (listbox) does not return anything.
I'm using "IHTMLDocument_getElementValueById" function to get data.

Any idea?
Thanks a lot
Eros



<html>
<head>
<meta http-equiv="MSThemeCompatible" content="Yes">
  <title>WebGui</title>

<style type="text/css">
<!--

#output
{
background: #FFFF00;
border: thin solid black;
text-align: center;
width: 300px;
}
-->
</style>

</head>
<body scroll="auto">


<p>Your Name: <Input Type="text" Size="15" Name="user-name">
<P>Customer Number: <Input Type="number" Size="10" Name= "customer-number">
<P>Shirt Size?
<Input Type="radio" Name="shirt-size" Value="S">S
<Input Type="radio" Name="shirt-size" Value="M">M
<Input Type="radio" Name="shirt-size" Value="L">L
<Input Type="radio" Name="shirt-size" Value="XL">XL
<P>What would you like?
<P><Select  Name="would-like" Size="3" multiple>
<Option>Order the product</Option>
<Option>Ask a question</Option>
<Option>Request a catalog</Option>
</Select>
<P>Your comments?<BR>
<TEXTAREA Name="comments" rows="4" cols="20"></TEXTAREA>
<P>

<br />
<div id="output" name="Output">
This is a DIV area
</div>
<br />


</body>
</html>
Title: Re: Getting data from web GUI elements other than textbox
Post by: José Roca on February 21, 2009, 12:17:56 AM
 
IHTMLDocument_getElementValueById will only work if the html element has an unique ID. None of your radio buttons has an identifier.
Title: Re: Getting data from web GUI elements other than textbox
Post by: Eros Olmi on February 21, 2009, 12:26:27 AM
Yes, you are right but I firstly tried with ID like in the following example but it does not work either when controls are Radio buttons or listboxes.
For radio buttons IHTMLDocument_getElementValueById always return the label of the radio, so in this case "M" or "S" but not its value: I would expect something like 0 not selected, 1 selected or something like that.
For listbox (select) nothing is returned.

Thanks
Eros

<html>
<head>
<meta http-equiv="MSThemeCompatible" content="Yes">
  <title>WebGui</title>

<style type="text/css">
<!--

#output
{
background: #FFFF00;
border: thin solid black;
text-align: center;
width: 300px;
}
-->
</style>

</head>
<body scroll="auto">


<p>Your Name: <Input Type="text" Size="15" Name="user-name">
<P>Customer Number: <Input Type="number" Size="10" Name= "customer-number">
<P>Shirt Size?
<Input Type="radio" Name="shirt-size" ID="SizeS" Value="S">S
<Input Type="radio" Name="shirt-size" ID="SizeM" Value="M">M
<Input Type="radio" Name="shirt-size" ID="SizeL" Value="L">L
<Input Type="radio" Name="shirt-size" ID="SizeXL" Value="XL">XL
<P>What would you like?
<P><Select Name="would-like" Size="3" ID="WouldLike" multiple>
<Option>Order the product</Option>
<Option>Ask a question</Option>
<Option>Request a catalog</Option>
</Select>
<P>Your comments?<BR>
<TEXTAREA Name="comments" rows="4" cols="20"></TEXTAREA>
<P>

<br />
<div id="output" name="Output">
This is a DIV area
</div>
<br />


</body>
</html>

Title: Re: Getting data from web GUI elements other than textbox
Post by: Eros Olmi on February 21, 2009, 12:36:03 AM
I suppose the problem is that for Radio and Select elements the attribute to return from the html element  is not "value" but something else.
I'm experimenting with this idea and maybe developing a IHTMLDocument_getElementValueById version that also pass the name of attribute to return.

Ciao
Eros

Title: Re: Getting data from web GUI elements other than textbox
Post by: Eros Olmi on February 21, 2009, 01:09:14 AM
Ok, I got it. For radio buttons I need to get "checked" attribute.
So I developed a little variation of "IHTMLDocument_getElementValueById" in which I also pass the attribute name to be returned.

Thanks a lot
Eros

Title: Re: Getting data from web GUI elements other than textbox
Post by: José Roca on February 21, 2009, 01:31:22 AM
 
I don't have expertise with html. If you can post some code that can improve the gui example it will be welcomed.
Title: Re: Getting data from web GUI elements other than textbox
Post by: Eros Olmi on February 21, 2009, 01:39:00 AM
Well, I'm learning a lot of things too thanks to your examples and it is a great pleasure been able to control such complex things.

Anyhow, I just developed a variation of "IHTMLDocument_getElementValueById" and called it "IHTMLDocument_getElementAttributeById".
With IHTMLDocument_getElementAttributeById you can pass also the name of the attribute to return.


Function IHTMLDocument_getElementAttributeById ( _
   ByVal pIHTMLDocument2 As IHTMLDocument2 _            ' [in] *IHTMLDocument <dispinterface>
, ByVal strId           As String _                    ' [in] <ANSI STRING> strId
, ByVal strAttr         As String _                    ' [in] <ANSI STRING> strId
   ) As String

   Local pIHTMLDocument3    As IHTMLDocument3           ' // Reference to the IHTMLDocument3 interface
   Local pIHTMLElement      As IHTMLElement             ' // Reference to the element
   Local vValue             As VARIANT                  ' // Value

   ' // Get a reference to the IHTMLDocument3 interface
   pIHTMLDocument3 = pIHTMLDocument2
   If IsFalse IsObject(pIHTMLDocument3) Then Exit Function

   ' // Get a reference to the input element
   pIHTMLElement = pIHTMLDocument3.getElementById(UCode$(strId))
   If IsFalse IsObject(pIHTMLElement) Then Exit Function

   ' // Get the value
   vValue = pIHTMLElement.getAttribute(UCode$(strAttr), 0)
   If ObjResult Then Exit Function

   ' // Convert the value to string
   If VariantVT(vValue) = %VT_Bool Then
      Function = LTrim$(Str$(CInt(Variant#(vValue))))
   ElseIf VariantVT(vValue) = %VT_BStr Then
      Function = Variant$(vValue)
   Else
      Function = LTrim$(Str$(Variant#(vValue)))
   End If

End Function


I suppose that (in order not to proliferate too much cloned functions) you can change IHTMLDocument_getElementValueById adding an "OPTIONAL BYVAL strAttr As String" parameter and inside the function if strAttr is "" than use "Value" otherwise use the requested attribute:
   ' // Get the value
   if strAttr = "" then strAttr = "Value"
   vValue = pIHTMLElement.getAttribute(UCode$(strAttr), 0)
   If ObjResult Then Exit Function


Ciao
Eros
Title: Re: Getting data from web GUI elements other than textbox
Post by: Eros Olmi on February 21, 2009, 10:23:19 AM
I was able to handle radio and check elements but not "Select" (listbox) elements.
I think every "option" inside a "select" list must be determined and checked if selected.

I found some interesting java script sources info here: http://www.javascript-coder.com/javascript-form/javascript-get-form.htm

I will post solution if I will success.