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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Online order form to only print items ordered 1

Status
Not open for further replies.

Pudsters

Technical User
Mar 16, 2006
151
US
I have an html Orderform for safety products for our employees, consisting of checkboxes. The user checks whichever items they want. No quantities are involved, it is always just 1 item.

Example:

HTML:
<input name="Butyl-Gloves" id="Butyl-Gloves" type="checkbox" value="yes">
<label><strong>Butyl Gloves</strong></label><br />

<input name="Face-Shield" id="Face-Shield" type="checkbox" value="yes">
<label><strong>Face Shield</strong></label><br />

<input name="Fire-Extinguisher-2.5Lb" id="Fire-Extinguisher-2.5Lb" type="checkbox" value="yes">
<label><strong>Fire Extinguisher (2-1/2LB)</strong></label><br />

<input name="Fore-arm-Protection" id="Fore-arm-Protectionb" type="checkbox" value="yes">
<label><strong>Fore-arm Protection</strong></label><br />

<input name="Goggles" id="Goggles" type="checkbox" value="yes">
<label><strong>Goggles</strong></label><br />

<input name="Leather-Gloves" id="Leather-Gloves" type="checkbox" value="yes">
<label><strong>Leather Gloves</strong></label><br />

<input name="Safety-Glasses" id="Safety-Glasses" type="checkbox" value="yes">
<label><strong>Safety Glasses</strong></label><br />

And so on, I have over 40 items.


The cgi script sends an autorespond email to the employee showing the results, but lists every item whether it was clicked or not! This makes it hard to find which items were requested. Example:

Butyl-Gloves =
Face-Shield =
Fore-arm-Protection = Yes
Goggles =
Leather-Gloves =
Safety-Glasses =

-end example-

How do I get it to just print the items ordered? In the example above, that woud be For-arm-Protection only.

And to take it one step forward. If someone ordered the first and last item, I wouldn't want any spaces between the items. It should look like this:

Butyl-Gloves = Yes
Safety-Glasses = Yes


I'm good with html, css, and just learning PHP now. Learning about If then statements and loops, but this cgi is way over my head.

I assume the part that needs to be changed is in this section of the script:

Any help would be greatly appreciated!

sub write_data
Perl:
{
   if ($fields{'submit_by'} ne "")
     {
       if (&valid_address == 0)   
        {
          &bad_email;
          exit;
        }
     }
   
   if ($fields{'submit_by'} ne "" && $fields{'emailfile'} ne "")
    {
      open (EMF,">>$fields{'emailfile'}");
      print EMF "$fields{'submit_by'}\n";
      close (EMF);
    }
   $the_date=localtime();
   if ($fields{'submit_to'} ne "")
    {
     $msgtext="";
     $msgtext .= "On $the_date,\n";
     $msgtext .=  "The following information was submitted:\n";
     $msgtext .=  "Host: $ENV{'REMOTE_ADDR'}\n";
    }
   if ($fields{'outputfile'} ne "")
    { 
      &get_the_lock; 
      open(OUT_FILE,">>$fields{'outputfile'}"); 
    }
   foreach $to_print (@sortlist)
    {
      if ($fields{'outputfile'} ne "")
       { print OUT_FILE "$fields{$to_print}\|"; }
      if ($fields{'submit_to'} ne "")
       { $msgtext .= "$to_print = $fields{$to_print}\n"; }
    }
   if ($fields{'outputfile'} ne "")
    {
     print OUT_FILE "$the_date\|\n";
     close(OUT_FILE);
     &drop_the_lock;
    }
   if ($fields{'submit_to'} ne "")
    {
      $mailresult=&sendmail($fields{submit_by}, $fields{submit_by}, $fields{submit_to}, $SMTP_SERVER, $fields{form_id}, $msgtext);
      if ($mailresult ne "1")
      {print "Content-type: text/html\n\n";
       print "MAIL NOT SENT. SMTP ERROR: $mailcodes{'$mailresult'}\n";
       exit
      }
    }
   if ($fields{'cc_to'} ne "")
    {
      $mailresult=&sendmail($fields{submit_by}, $fields{submit_by}, $fields{cc_to}, $SMTP_SERVER, $fields{form_id}, $msgtext);
    }
}

Not sure if this next part has anything to do with it?

sub decode_vars
Perl:
 {
  $i=0;
  read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
  @pairs=split(/&/,$temp);
  foreach $item(@pairs)
   {
    ($key,$content)=split(/=/,$item,2);
    $content=~tr/+/ /;
    $content=~s/%(..)/pack("c",hex($1))/ge;
    $content=~s/\t/ /g;
    $fields{$key}=$content;
    if ($key eq "data_order")
      {
       $content=~s/ //g;
       @sortlist=split(/,/,$content);
      }
    if ($key eq "required")
      {
       $content=~s/ //g;
       @mandatory=split(/,/,$content);
      }
   }
if  (
     ( ($fields{automessage}=~ /^([-\/\w.]+)$/ || $fields{automessage} eq "") &&
      ($fields{countfile}=~ /^([-\/\w.]+)$/ || $fields{countfile} eq "") &&
      ($fields{emailfile}=~ /^([-\/\w.]+)$/ || $fields{emailfile} eq "") &&
      ($fields{outputfile}=~ /^([-\/\w.]+)$/ || $fields{outputfile} eq "") )
        &&
     ( (substr($fields{automessage},0,1) ne "/") &&
       (substr($fields{countfile},0,1) ne "/") &&
       (substr($fields{emailfile},0,1) ne "/") &&
       (substr($fields{outputfile},0,1) ne "/") )
    )
    {$donothing=0;}
    else
     {
       print "Content-type: text/html\n\n sorry, invalid characters...\n";
       exit;
     }
}
 
Hi

I would try to add the [highlight]highlighted[/highlight] piece :
Code:
   [b]foreach[/b] [navy]$to_print[/navy] [teal]([/teal][navy]@sortlist[/navy][teal])[/teal]
    [teal]{[/teal]
      [b]if[/b] [teal]([/teal][navy]$fields[/navy][teal]{[/teal][i][green]'outputfile'[/green][/i][teal]}[/teal] ne [i][green]""[/green][/i][teal])[/teal]
       [teal]{[/teal] [b]print[/b] OUT_FILE [i][green]"$fields{$to_print}\|"[/green][/i][teal]; }[/teal]
      [b]if[/b] [teal]([/teal][navy]$fields[/navy][teal]{[/teal][i][green]'submit_to'[/green][/i][teal]}[/teal] ne [i][green]""[/green][/i] [highlight][teal]&&[/teal] [navy]$fields[/navy][teal]{[/teal][navy]$to_print[/navy][teal]}[/teal] ne [i][green]""[/green][/i][/highlight][teal])[/teal]
       [teal]{[/teal] [navy]$msgtext[/navy] [teal].=[/teal] [i][green]"$to_print = $fields{$to_print}\n"[/green][/i][teal]; }[/teal]
    [teal]}[/teal]


Feherke.
feherke.github.io
 
OMG, I can't believe that is all that was needed! It worked!

Thank you so much. Are you able to explain how that works?

I assume it means any fields that are Not null.

Scott
 
Hi

Yes, the %fields hash contains the checkbox data as checkbox name - value pairs. Then the loop processes each entry having the checkbox name in the $to_print variable, so the matching value can be accessed as $fields{$to_print}. As the value can be either "yes" or "" and you not want those with "", the condition just avoids those being appended to the mail body.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top