如何遍历Perl CGI脚本中的所有复选框组的值? [英] How can I iterate through all checked values of a checkbox group in a Perl CGI script?

查看:298
本文介绍了如何遍历Perl CGI脚本中的所有复选框组的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows计算机上运行了 xampp 测试系统设置。



我有一个小的命令行应用程序,我想使用
Perl CGI脚本触发。



有一个形式,我创建使用Perl。有多个复选框以及用户可以输入消息的输入框。



这里是我到目前为止的代码。我不是任何方式的perl专家,所以有
可能是一些事情在这里没有意义,或用于测试目的

 #!C:\xampp\perl\bin\perl.exe

#print一个标准的200级HTTP标头
printContent-Type:text / html \\\
\\\
;

print< html>< head>< title> Broadcast Message< / title>< / head> \\\

print< body> \\\
;

#display form data
#& displayInfo();

print< / body>< / html> \\\
;

if($ ENV {REQUEST_METHOD} eqGET)
{
& formDisplay();
exit;
}
#Else处理并显示回屏幕
else
{
#& switches();
& parseform();
& displayInfo();

exit;
}

#此子程序将不使用HTML文档显示信息

sub formDisplay
{
print qq〜

< FORM METHOD =POSTACTION =/ cgi-bin / broadcast.cgi>
< h3>广播消息到域< / h3>
***请注意***这将向域中的所有计算机广播一条消息< / br>< / br>
< TABLE cellpadding = 0 cellspacing = 1 border = 0>
< TR>
< TD>通过开关选择您的目标:& nbsp& nbsp& nbsp& nbsp& nbsp& nbsp& nbsp& nbsp< / br>< / TD&
< TD>< / TD>
< TD>按部门选择您的目标:< / 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>
请输入您希望广播到域的邮件:< / br>
< input name =message>< / br>

< input type = SUBMIT name = Send value =Send>
< input type = RESET name = Reset value =Reset>
< / form>
〜;
}

#这个子程序将显示从表单
接收的信息sub displayInfo {
print< b>您的消息:< / b& / br>,$ form {message},< / br>;
$ system_message = $ form {message};
#systemsent.exe / u:testmachine \$ system_message\;
print qq〜
< form>
< input type =buttonvalue =Backonclick =history.go(-1); return true;>
< / form>
〜;
#表单散列的工作循环
delete $ form {'Send'};

foreach(keys%form){
print$ form {$ _}< / br>;
}
#############################
$ textme = $ form { testfiletxt};
open(FILE,< $ textme)或die无法打开$ filename:$!

while(< FILE>){
chomp;
printsent.exe $ _,$ form {message},< / BR>;
}

close FILE;

}


sub parseform
{
#get环境变量中的数据
读取STDIN,$ qstring,$ ENV {CONTENT_LENGTH};
#$ qstring = $ ENV {'QUERY_STRING'};

#将数据放在&符号上,并存储在数组
@pairs = split(/& /,$ qstring);

#start一个循环来处理表单数据
foreach(@pairs){
#split字段名称和值在=,存储在两个标量变量
($ key,$ value)= split(/ = /);
#translate'+'回到空格
$ value =〜tr / + / /;
#translate特殊字符
$ value =〜s /%([a-fA-F0-9] [a-fA-F0-9])/ pack(C )/例如;
#存储哈希中的数据
$ form {$ key} = $ value;
}
}

我遇到的问题是处理文本框



我需要脚本做的是使用用户指定的参数运行命令行实用程序(即复选框和输入框)。



我认为这将如何工作如下:


  1. 我们预先建立了文字档案,其中会包含 Netbios名称,每行一个计算机名称。这些文件将表示开关,每个开关文件将包含连接到它的计算机的Netbios名称。


  2. 用户将转到此广播页面,并选择要广播的交换机。因此,连接到特定交换机的任何机器将接收该广播的消息。


  3. 当用户点击提交时,需要执行一些操作。




    • 需要打开表示所选开关的文件,并且用户输入的消息需要附加到列表中每个Netbios名称的末尾。示例:



    用户提供的netbiosName消息




    • 然后,一旦完成。命令行实用程序需要使用我们刚才编辑的文件以下列格式运行:

        sent.exe / t: 15 / f:$ filename 




    用户选择3个开关命令应该运行3次

      send.exe / t:15 / f:swtich1 
    sent.exe / t:15 / f:switch2
    sent.exe / t:15 / f:switch3


我应该指出, / f 选项告诉发送。 exe 命令使用需要以特定方式格式化的文件。



我在处理一个选项时取得了一些成功



但是我遇到了当用户选择多个复选框时会发生什么。



如何使用循环方便地处理所选的复选框?



我可以从& parseform

使用 .org / module / CGIrel =nofollow> CGI.pm

  my @checked = $ cgi-> param('department'); 

将为您提供所有选中的复选框,名称为department。要执行此操作,您需要清理HTML:

 < input type =checkboxname =department =517
id =department_517>< label for =department_517> 517< / label>

< input type =checkboxname =departmentvalue =ACC
id =department_ACC>< label for =department_ACC> ACC<标签>

< input type =checkboxname =departmentvalue =testfiletxt
id =department_testfiletxt>< label
for =department_testfiletxt> ; testfile.txt< / label>


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
#&displayInfo();

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:&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp   </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:

  1. 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.

  2. 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.

  3. 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>

这篇关于如何遍历Perl CGI脚本中的所有复选框组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆