简体   繁体   中英

Perl CGI, Multiple forms and multiple submit button with action on the same page

How do I specify which action is taken hitting one of the multiple buttons on my Perl CGI HTML output given I have several buttons and "action=" is also on the same page?

This is the html output and the buttons(Process Activity and Duplicate record) work correctly but "Import Info"(which I'm trying to implement) calls the "Process Activity"

here is the java-script code:

<script type="text/javascript" src="$HostedSiteURL/$ScriptDirectory/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
\$(document).ready(function() {

\$('#importFromCAD').click(function () {
    \$('#importNav').val('');
    return true;

\$('#process-activity').click(function () {
    \$('#DupNav').val('');
    return true;
});

And the Perl CGI HTML Code:

sub NewRightSide
{
  print "   <div style=\"z-index:86;\" class=\"group-shell\">";
  print "   <table>\n";
  print "   <tr><td><input class=\"dial-red-button\" id=\"importFromCAD\"  type=\"submit\" value=\"Import Info\"></td></tr>\n";
  print "   <tr><td><input class=\"dial-red-button\" id=\"process-activity\" type=\"submit\" value=\"Process Activity\"></td></tr>\n";
  print "   <tr><td><input class=\"dial-red-button\" id=\"duplicate-record\" type=\"submit\" value=\"Duplicate Record\"></td></tr>\n";

picture of button layout

I believe this is the form which gets called:

 print "<form name=\"form\" accept-charset=\"utf-8\" method=\"post\"    action=\"A_CT_DIAL8.pl\">\n";
 if ($Nav eq "" || $Nav eq "None")         {$Nav="NewEntry";}
 print "<input type=\"hidden\" name=\"s\" value=\"$escape_session\" />\n";
 print "<input type=\"hidden\" name=\"nav\" value=\"DIAL\" id=\"nav\">\n";
 print "<input type=\"hidden\" name=\"Nav\" value=\"$Nav\" id=\"Nav\">\n";
 print "<input type=\"hidden\" name=\"SubNav\" value=\"$SubNav\">\n";
 print "<input type=\"hidden\" name=\"DupNav\" value=\"\" id=\"DupNav\">\n";
 print "<input type=\"hidden\" name=\"nav_tab\" value=\"\" id=\"nav_tab\">\n";
 print "<input type=\"hidden\" name=\"Report\" value=\"\" id=\"Report\">\n";
 print "<input type=\"hidden\" name=\"TransLimit\" value=\"$TransLimit\">\n"; 

This is the Perl Subroutine called "DupNav" which I'm not sure it plays a role in how the form functions. Is this the subroutine the second .click(function ()) class?

  if ($DupNav eq "")
  {
     $Nav           = "";    $KeyField      = "";  # $CAD              = "";
     $In            = "";    $Out           = "";    $Via              = "";
     $Status        = "";    $Device        = "";    $ActivitySubject  = "";
     $Memo          = "";    $currenttime   = "";    $NormalMemo       = "";
     $CheckNewMemo  = "";    $PostMile      = "";
   }
   else
   {
      $CheckRadio="No";
   if ($DupWarn ne "Off")
   {
    $JavaWarn=$JavaWarn."Duplicated Last Entry.             ";
    $Warn=$Warn." [ Duplicated Last Entry ]";
    $SubNav="Go";
    }
    else
    {
    $JavaWarn=$JavaWarn."Use the Duplicate Record button to pre fill the         next entry with the same information as the last entry.             ";
    $Warn=$OldWarn." [ Use the Duplicate Record button to pre fill the next   entry with the same information as the last entry ]";
    }
  }
  if ($Device == 0) {$Device="";}
  $currentdate   = "";


  $SplitMemo=$CheckNewMemo;
  @GetEntries=split(":DOSEP:", $SplitMemo);
  $EntryCount=@GetEntries;
  $Memo=$GetEntries[0];

  $b=1;
  while ($b < $EntryCount)
  {
  $SplitExtras=$GetEntries[$b];
  @GetExtras=split(":", $SplitExtras);
  $ExtraListName=$GetExtras[0];
  $ExtraListInfo=$GetExtras[1];
   if ($ExtraListName eq "PostMile") {$PostMile=$ExtraListInfo;             $DisablePostMileSection="No";}else{$Extra_Information{$ExtraListName}="$ExtraLis     tInfo";}
 $b++;
 }

}

I know this is very long and I'd really appreciate any feedback I can get. I can post additional information as required. Thank you again.

Give all your submit buttons unique name attributes, the submission data will have only one submit parameter, the one that was clicked. Find out what it is by checking for their name s and process accordingly. Some example code is given below.

Client Side:

<form method="POST" action="/act">
    <input name="formid" value="1" type="hidden">
    <input class="delete" value="D" name="delete" type="submit">
    <input class="edit" value="E" name="edit" type="submit">
</form>

Server Side:

if ( defined param('edit')) {
    # perhaps identify form by some checking for some hidden element
    # process the data for edit
}
elsif ( defined param('delete') ) {
    # perhaps identify form and process the data for delete
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM