How can I iterate through all checked values of a checkbox group in a Perl CGI script?
I have an xampp test system setup running on a Windows machine.
I have a small command line application I would like to trigger using a Perl CGI script.
I have a form that I created using Perl. There are a number of check boxes as well as an input box where a user can type a message.
Here is the code that I have so far. I am not an expert in perl by any means, so there might be a few things in here that don't make sense, or are for testing purposes
#!"C:\xampp\perl\bin\perl.exe"
#print a standard 200 -level HTTP header
print "Content-Type:text/html\n\n";
print "<html><head><title>Broadcast Message</title></head>\n";
print "<body>\n";
#display form data
#&displayInf开发者_StackOverflowo();
print "</body></html>\n";
if ($ENV{REQUEST_METHOD} eq "GET")
{
&formDisplay();
exit;
}
#Else process and display back to screen
else
{
#&switches();
&parseform();
&displayInfo();
exit;
}
#This subroutine will display information without using the HTML document
sub formDisplay
{
print qq~
<FORM METHOD="POST" ACTION="/cgi-bin/broadcast.cgi">
<h3>Broadcast Message to Domain</h3>
***PLEASE NOTE*** This will broadcast a message to ALL machines in the Domain</br></br>
<TABLE cellpadding=0 cellspacing=1 border=0>
<TR>
<TD>Select your target by switch:             </br></TD>
<TD></TD>
<TD>Select your target by department: </br></TD>
</TR>
<TR>
<TD><input type=CHECKBOX name=517 value="517"> - 517 </br></TD>
<TD></TD>
<TD><input type=CHECKBOX name=ACC value="ACC"> - ACC </br></TD>
</TR>
<TR>
<TD><input type=CHECKBOX name=testfiletxt value="testfiletxt"> - testefiletxt </br></TD>
</TR>
</TABLE>
</br></br>
Please type the message you wish to broadcast to the Domain: </br>
<input name="message"></br>
<input type=SUBMIT name=Send value="Send">
<input type=RESET name=Reset value="Reset">
</form>
~;
}
#This subroutine will display information received from a form
sub displayInfo {
print "<b>Your Message:</b> </br> ", $form{"message"}, "</br>";
$system_message=$form{"message"};
#system "sent.exe /u:testmachine \"$system_message\"" ;
print qq~
<form>
<input type="button" value="Back" onclick="history.go(-1);return true;">
</form>
~;
# Working loop for form hash
delete $form{'Send'};
foreach(keys %form) {
print "$form{$_} </br>";
}
##############################
$textme=$form{"testfiletxt"};
open( FILE, "< $textme" ) or die "Can't open $filename : $!";
while( <FILE> ) {
chomp;
print "sent.exe $_ ", $form{"message"} , "</BR>";
}
close FILE;
}
sub parseform
{
#get data from environment variable
read STDIN,$qstring,$ENV{"CONTENT_LENGTH"};
#$qstring = $ENV{'QUERY_STRING'};
#break data up on ampersands, and store in array
@pairs = split(/&/, $qstring);
#start a loop to process form data
foreach (@pairs) {
#split field name and value on "=", store in two scalar variables
($key, $value) = split(/=/);
#translate '+' signs back to spaces
$value =~ tr/+/ /;
#translate special characters
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#store data in hash
$form{$key} = $value;
}
}
What I am having issues with is processing the text boxes that have been checked.
What I need the script to do is to run the command line utility with the parameters specified by the user (i.e. the check boxes, and input box).
How I am thinking this will work is as follows:
We have pre built text files that will contain Netbios names of computers in our network, one computer name per line. These files will represent switches, each switch file will contain the Netbios name of the computers attached to it. We will eventually be expanding this into doing groups as well.
The user will go to this broadcast page and select the switches they wish to broadcast to. So, any machine connected to a particular switch will receive this broadcasted message. The user will also provide a message to be broadcast.
When the user clicks "Submit" There are a few things that need to happen.
- The file representing the selected switch will need to be opened and the message that the user entered needs to be appended to the end of each Netbios name in the list. Example:
netbiosName message provided by user
Then, once that is completed. The command line utility needs to be run using the files that we just edited in the following format:
sent.exe /t:15 /f:$filename
So if the user selected 3 switches the command should be run 3 times
sent.exe /t:15 /f:swtich1 sent.exe /t:15 /f:switch2 sent.exe /t:15 /f:switch3
I should point out that the /f
" option tells the sent.exe
command to use a file that is required to be formatted in a specific way.
I have had some success with processing one option at a time.
But I am stuck with what will happen when the user selects more then one check box.
How can I process the selected check boxes easily using a loop?
Can I pull this from the &parseform
sub-routine? Or is there an easier way of doing it?
Using CGI.pm:
my @checked = $cgi->param('department');
will give you all the checked checkboxes with name 'department'. To do this, you need to clean up your HTML:
<input type="checkbox" name="department" value="517"
id="department_517"><label for="department_517">517</label>
<input type="checkbox" name="department" value="ACC"
id="department_ACC"><label for="department_ACC">ACC</label>
<input type="checkbox" name="department" value="testfiletxt"
id="department_testfiletxt"><label
for="department_testfiletxt">testfile.txt</label>
精彩评论