Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read checkbox values from web page

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi Everyone!
I have been away from this for a long time now and am drawing blanks for something that should be fairly easy.

I need to read all of the check boxes in a web form and update a text file responding to the name of each checkbox. This is being done locally with a .hta file so file access is not an issue.
What I really need to know is not the read/write but just how to read the values from the checkboxes.

What is the best way to cycle through the checkboxes on the form? I have the names of them in an array already but is that the best way to go or is it easier to sequentially read all checkboxes on the page?
Any simple way to read only checkboxes where values have changed? I cannot use an onchange event directly because they need to be able to check a bunch of boxes then perform the action that will apply to all that have been checked.

Thanks in advance.

Nite.

At my age I still learn something new every day, but I forget two others.
 
Well, it's a bit hard to tell you the best way to do this without seeing the hta but.....

HTA's can include vbscript code inside the HTA itself as you're probably aware. Since you cannot use an onchange event because you have to wait until multiple boxes are checked you need to have a button on the form itself that the user can press to indicate that they're done. Associated with the button you will build an subroutine (i.e. Sub lnkExecute_OnClick(). You'll DIM a variable (i.e. Form) inside the subroutine, you'll set your form variable to whatever you called your form in the HTA (i.e. Set form = document.ExDomform and from there it's just a matter of reading the check boxes to see what's checked and what's not through IF statements.

If Form.ComCheckbox.Checked Then
Do something
end if

Once you you get to this point the order that you specify the IF statements is meaningless unless the action of checking on check box means you'll do something else if other check boxes are checked. Personally I don't see any advantage of having the names of the check boxes in in an array so I would keep it simple and just process them in the order that makes most sense to you. The difficult thing is to keep track of your conditions if different actions are to take place based on whether a combination of checkboxes are checked or not checked. If order isn't important to the processing of the request, don't worry about the order of the if statements.

The only other thing, in my opinion you could do if you need to is use an onchange subroutine on each checkbox to set the value of a global variable associated with each check box. For example you could have a global variable setup (i.e. bolComCheckBoxChecked) in the HTA and in the onchange subroutine associated with that checkbox you could put the IF statement above in that subroutine to set the Boolean variable to true or false. Quite frankly, I wouldn't waste my time on this avenue unless you need to manipulate your form (graying out fields, checkboxes and radio button) based on what happens when a check boxes gets checked. For example I had a form that had many check boxes, radio buttons and text fields on it but as the user was entering the form I was controlling the various fields, checkboxes and radio buttons to make sure that they could not select or enter something that was inappropriate based on a prior selection that they made. The HTA worked slick but it was time consuming setting up all of the conditions to work correctly. Again, if you don't have to go this route keep it simple.

Hope this helps.
 
Hi [peace]
Two days ago, i posted in a french forum a HTA dealing with checkboxes that can be dynamically created from a text file.
The text file is created by default is "Process2Kill.txt" by the program and contains the names of process per lines that can be read as values of the checkbox, and you can add, modify or delete some other process to be killed manually from this file : "Process2Kill.txt" after its creation of course, and the HTA can recreate the checkbox once again dynamically from it.
Hope this HTA can provide you an idea of how to create and read the values of the checkbox dynamically from any text file.
Good Luck for you !
505557867.gif


Code:
<Html>
<Head>
<Title>PC Information + Process Killer © Hackoo Crackoo © 2013</Title>
<HTA:Application
Caption = Yes
Icon = "Explorer.exe"
Border = Thick
ShowInTaskBar = Yes
SingleInstance = Yes
MaximizeButton = Yes
MinimizeButton = Yes>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES">
<script Language = VBScript>
Option Explicit
Dim fso,Titre,LogFile
Titre = "PC Information + Process Killer© Hackoo Crackoo © 2013"
Set fso = CreateObject("Scripting.FileSystemObject")
LogFile = "ProcessKilled.txt"
If fso.FileExists(LogFile) Then fso.DeleteFile LogFile
'**********************************************************************************************
Sub Window_OnLoad
	Dim intWidth,intHeight,strIPInfo,arrIPInfo
	intWidth = 800
	intHeight = 600
	Me.ResizeTo intWidth, intHeight
	Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
	span_computername.innerHTML = GetComputerName()
	span_username.innerHTML = GetUserName()
	strIPInfo = GetIPInformation()
	If InStr(strIPInfo, ";") > 0 Then
		arrIPInfo = Split(strIPInfo, ";")
		If UBound(arrIPInfo) >= 3 Then
			span_ipaddress.InnerHTML = arrIPInfo(0)
			span_subnetmask.InnerHTML = arrIPInfo(1)
			span_defaultgateway.InnerHTML = arrIPInfo(2)
			span_primarydns.InnerHTML = arrIPInfo(3)
			If UBound(arrIPInfo) = 4 Then
				span_secondarydns.InnerHTML = arrIPInfo(4)
			End If
		End If
	End If
	GenererCheckBox()
End Sub
'**********************************************************************************************
Function GetComputerName()
	Dim objNetwork
	Set objNetwork = CreateObject("WScript.Network")
	GetComputerName = objNetwork.ComputerName
End Function
'**********************************************************************************************
Function GetUserName()
	Dim objNetwork
	Set objNetwork = CreateObject("WScript.Network")
	GetUserName = objNetwork.UserName
End Function
'**********************************************************************************************
Function GetIPInformation()
	Dim strComputer,objWMIService,colNetAdapters,strDetails,objAdapter
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colNetAdapters = objWMIService.ExecQuery _
	("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True")
	strDetails = ""
	For Each objAdapter In colNetAdapters
		If IsNull(objAdapter.IPAddress) = False And IsNull(objAdapter.DefaultIPGateway) = False Then
			If Join(objAdapter.IPAddress) <> "0.0.0.0" Then
				If strDetails = "" Then
					strDetails = Join(objAdapter.IPAddress) & ";"
					strDetails = strDetails & Join(objAdapter.IPSubnet) & ";"
					strDetails = strDetails & Join(objAdapter.DefaultIPGateway) & ";"
					strDetails = strDetails & Join(objAdapter.DNSServerSearchOrder, ";")
				End If
			End If
		End If
	Next
	GetIPInformation = strDetails
End Function
'**********************************************************************************************
Sub SelectAll()
	Dim checkbox
	For Each checkbox In CheckboxOption
		checkbox.Checked = True 
	Next
End Sub
'**********************************************************************************************
Sub UnSelectAll()
	Dim checkbox 
	For Each checkbox In CheckboxOption
		checkbox.Checked = False 
	Next
End Sub
'**********************************************************************************************
Sub KillThis()
	Dim LogFile,ws,fso,checkbox
	LogFile = "ProcessKilled.txt"
	Set Ws  = CreateObject("Wscript.Shell")
	Set FSO = CreateObject("Scripting.FileSystemObject")
	For Each checkbox In CheckboxOption
		If checkbox.Checked = True Then 
			Kill(checkbox.Value)
		End If
	Next
	If FSO.FileExists(LogFile) Then
		MsgBox Formater(LogFile),64,Titre
		ws.run LogFile,1,True
	End If
End Sub
'**********************************************************************************************
Sub Kill(Process)
	Dim FSO,Ws,MyDate,Command,Execution,LogFile
	Set FSO = CreateObject("Scripting.FileSystemObject")
	Set Ws  = CreateObject("Wscript.Shell")
	LogFile = "ProcessKilled.txt"
	MyDate = "cmd /c echo %date% ^@ %time%  >> "&LogFile&""
	Command = "cmd /c Taskkill /F /IM "&Process&" >> "&LogFile&""
	Execution = Ws.Run(MyDate,0,True)
	Execution = Ws.Run(Command,0,True)
End Sub
'**********************************************************************************************
'Fonction pour formater et remplacer les caractères spéciaux unicode dans le LogFile
Function Formater(LogFile)
	Dim fso,fRead,fWrite,Text
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set fRead = fso.OpenTextFile(LogFile,1)
	Text = fRead.ReadAll
	fRead.Close
	Set fWrite = fso.OpenTextFile(LogFile,2,True)
	Text = Replace(Text,"‚","é")
	Text = Replace(Text,"ÿ"," ")
	Text = Replace(Text,"ˆ","ê")
	Text = Replace(Text,"‡","ç")
	Text = Replace(Text,"“","ô")
	Text = Replace(Text,"…","à")
	Text = Replace(Text,"Š","è")
	Text = Replace(Text,"ƒ","â")
	Text = Replace(Text,"?"," ")
	fWrite.WriteLine Text
	Formater = Text
End Function
'**********************************************************************************************
'Fonction pour ajouter les doubles quotes dans une variable
Function DblQuote(strIn)
	DblQuote = Chr(34) & strIn & Chr(34)
End Function
'**********************************************************************************************
Sub GenererCheckBox()
	Const ForReading = 1
	Dim StrHTML,objFSO,strNewFile,ListProcess,objFile,LireTout,MyTab,i
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	StrHTML = "<h2><b><font color='red'>Processus à Arrêter</font></b></h2>"
	strNewFile = "Process2Kill.txt"
	If Not objFSO.FileExists(strNewFile) Then
			ListProcess = "iexplore.exe" & vbCrLf & "FireFox.exe" & vbCrLf & "Chrome.exe" & vbCrLf &_
			"outlook.exe" & vbCrLf & "winword.exe" & vbCrLf & "excel.exe" & vbCrLf & "searchindexer.exe" & vbCrLF & "searchprotocolhost.exe"
			Call WriteProcessFile(ListProcess,strNewFile)
	End If
	Set objFile = objFSO.OpenTextFile(strNewFile,ForReading)
	LireTout = objFile.ReadAll
	MyTab = Split(LireTout,vbCrLf)
	For i = LBound(MyTab) To UBound(MyTab)
		StrHTML = StrHTML & MySpanCheckBox.InnerHTML &_
		"<br><input type='checkbox' name='CheckboxOption' value=" & MyTab(i) & " title = " & MyTab(i) & " Checked = 'True'>" & MyTab(i)
	Next
	MySpanCheckBox.InnerHTML = StrHTML
End Sub
'**********************************************************************************************
'Fonction pour écrire les processus par défaut dans un fichier texte
Sub WriteProcessFile(strText,File)
	Dim fs,ts 
	Const ForWriting = 2
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set ts = fs.OpenTextFile(File,ForWriting,True)
	ts.Write strText
	ts.Close
End Sub
'**********************************************************************************************
</script>
<body text="white" style="background-color:DarkOrange">
<table width= "90%" border="0" align="center">
<tr>
<td align="center" colspan="2">
<h2>PC INFORMATION</h2><br>
</td>
</tr>
<tr>
<td>
Le Nom de l'ordinateur est : 
</td>
<td>
<span id="span_computername"></span>
</td>
</tr>
<tr>
<td>
Vous êtes connecté en tant que :
</td>
<td>
<span id="span_username"></span>
</td>
</tr>
<tr>
<td>
Adresse IP est : 
</td>
<td>
<span id="span_ipaddress"></span>
</td>
</tr>
<tr>
<td>
Le masque de sous-réseau est :
</td>
<td>
<span id="span_subnetmask"></span>
</td>
</tr>
<tr>
<td>
Passarelle par défaut est :
</td>
<td>
<span id="span_defaultgateway"></span>
</td>
</tr>
<tr>
<td>
Serveur DNS Primaire  est :
</td>
<td>
<span id="span_primarydns"></span>
</td>
</tr>
<tr>
<td>
Serveur DNS Secondaire  est :
</td>
<td>
<span id="span_secondarydns"></span>
</td>
</tr>
</table>
</body>
<center><input type="button" name="SelectAll" id="SelectAll" value="Tout sélectionner" onclick="SelectAll">
<input type="button" name="UnSelectAll" id="UnSelectAll" value="Tout désélectionner" onclick="UnSelectAll">
<input type="button" name="KillThis" id="KillThis" value="Arrêter le(s) Processus" onclick="KillThis()">
<table>
<tr>
<td>
<span id="MySpanCheckBox"></span>
</td>
</tr>
</table>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top