Solved

trouble with optional and required fields in a form

Posted on 2002-04-08
11
214 Views
Last Modified: 2011-09-20
I have this script from an e-mail form:

#!/usr/local/bin/perl

$|++;      ##      Disable output buffering.

use CGI;

$q=new CGI;

$err_ctr=0;      ##      Initailize the error count.

##      Read the incoming HTML variables into Perl variables.
##      The Perl variables have the same name as the HTML form variables.
foreach ($q->param){
      $$_=$q->param($_);
      $$_=~ s/^\s+//g;
      $$_=~ s/\s+$//g;

      if (/^cp$/ || /^celular$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){
 next;      ##      Ignore this optional field.
      if (!($$_)){      ##      Check if any of the mandatory fields are empty.
            print "Content-type: text/html\n\n" if (!($err_ctr));
            print "<B><FONT COLOR=RED>$_ cannot be empty.</FONT></B><BR>\n";
            ++$err_ctr;      ##      Increment the error count.
      }else{
            ##      Build the plain text message of the email.
            $plain_text.=$_." = ".$$_."\n";
            #print $_," = ",$$_,"<BR>\n";
      }
}

if ($err_ctr){      ##      We do have some error messages!!
      exit;      ##      Bail out!!
}

$boundary="Message-Boundary-19990614";
$from='Web<>';

push(@attachments,$file);

##      Prepare to send out the email to you, the owner of the site.
$mailprog ="/usr/lib/sendmail";
open(MAIL, "| $mailprog -t ") || die "Content-type: text/html\n\n$!";
print MAIL "To: $recipient\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject!\n";
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-type: Multipart/1;\n";
print MAIL "\tboundary=$boundary\n";
print MAIL "\n";
print MAIL "\n";
print MAIL "--$boundary\n";

##      Add the plain text message to the email
if ($plain_text){
      print MAIL "Content-type: text/plain; charset=US-ASCII\n";
      print MAIL "Content-description: Mail message body\n";
      print MAIL "Content-transfer-encoding: 7BIT\n";
      print MAIL "\n";
      print MAIL $plain_text."\n";
      print MAIL "--$boundary\n";
}

##      Attach all the files to the email
foreach $file (@attachments){
      undef $some_text;

      ##  Read 1024 bytes at a time
      while($bytesread=read($file,$data,1024)){
            $content.=$data;    ##  Store the ACTUAL file content
      }
      close($file);

      $some_text=&encode_base64($content);

      ##      Convert all \'s to /
      $file=~ s/\\/\//g;

      ##      Extract the name of the file.
      $file=~ /.*\/(.*)/;
      $actual_file=$1;

      ##  Print the header for that attachment.
      print MAIL "Content-type: application/octet-stream; name=\"".$actual_file."\"; type=Unknown\n";
      print MAIL "Content-transfer-encoding: BASE64\n";
      print MAIL "Content-disposition: attachment\n";
      print MAIL "\n";
            
      ##  Send out the contents!!
      print MAIL $some_text;
      print MAIL "\n";
      print MAIL "--$boundary\n";
}

##      Send the Email!!
print MAIL "--$boundary--\n";
close(MAIL);

##      Prepare to send out the "Thank you" email to the user.
$mailprog ="/usr/lib/sendmail";
open(MAIL, "| $mailprog -t ") || die "Content-type: text/html\n\n$!";
print MAIL "To: $E_MAIL\n";
print MAIL "From: $from\n";
print MAIL "Subject: Gracias por llenar nuestro formulario. \n";
print MAIL "Estimado usuario:\n\nSu información ha sido recibida.\nEn breve nos comunicaremos con usted.\n\nAtentamente\n\nSelective Directory";
print MAIL "\n";
close(MAIL);

##      Now redirect the user to some page.
print "Location: $redirect\n\n";

sub encode_base64 ($;$){
  my $res = "";
  my $eol = $_[1];
  $eol = "\n" unless defined $eol;
  pos($_[0]) = 0;                          # ensure start at the beginning
  while ($_[0] =~ /(.{1,45})/gs) {
    $res .= substr(pack('u', $1), 1);
    chop($res);
  }
  $res =~ tr|` -_|AA-Za-z0-9+/|;               # `# help emacs
  # fix padding at the end
  my $padding = (3 - length($_[0]) % 3) % 3;
  $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
  # break encoded string into lines of no more than 76 characters each
  if (length $eol) {
    $res =~ s/(.{1,76})/$1$eol/g;
  }
  $res;
}


but it sends an error:

Missing right curly or square bracket at /home/cgi-bin/head_hunters1.pl line 129, at end of line
syntax error at /home/cgi-bin/head_hunters1.pl line 129, at EOF
Execution of /home/cgi-bin/head_hunters1.pl aborted due to compilation errors.

the only part of the script I changed is the one about optional fields:

if (/^cp$/ || /^celular$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){
next;     ##     Ignore this optional field.

but if I write this instead:

next if /^cp$/;
next if /^celular$/;
next if /^CP$/;
next if /^tel_oficina$/;

it doesn´t sends an error, and it doesn´t ask for the fields to be filled, but if I fill them when I receive the email from the form the information from those fields don´t appear in it
0
Comment
Question by:nadiassdcg
  • 6
  • 4
11 Comments
 

Author Comment

by:nadiassdcg
ID: 6926798
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6926852
nadiassdcg,

"..but if I fill them when I  receive the email from the form the information from those fields don´t appear in it .."

REPLACE...

if (/^cp$/ || /^celular$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){
                     next;     ##     Ignore this optional field.


WITH...

if (/^cp$/ || /^celular$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){
                     next;     ##     Ignore this optional field.
}
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6926856
nadiassdcg,

i have fixed that error in the script & tested it.

It works fine.

Let me know if that is indeed the case.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:nadiassdcg
ID: 6926877
maneshr,

the error is fixed, but the information from the optional fields isn´t arriving with the info from the form
0
 

Author Comment

by:nadiassdcg
ID: 6926990
I added file:

if (/^cp$/ || /^celular$/ || /^file$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){next;     ##     Ignore this optional field

}

and though the atachment arrives without problem, and there´s no error if I send no atachment;  the information from: cp, celular, tel-oficina, extension, dia_hora_entrega isn´t arriving in the e-mail from the form
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6927400
nadiassdcg,

"..the  information from: cp, celular, tel-oficina, extension, dia_hora_entrega isn´t arriving in the e-mail from the form ..."

To fix this, ADD the following line..

$plain_text.=$_." = ".$$_."\n";

AFTER...

if (/^cp$/ || /^celular$/ || /^file$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){next;
0
 
LVL 84

Expert Comment

by:ozo
ID: 6927455
$$_=$q->param($_); #this is not recommended
0
 

Author Comment

by:nadiassdcg
ID: 6930044
I added $plain_text.=$_." = ".$$_."\n";

like this:

if (/^cp$/ || /^celular$/ || /^file$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){next;
##     Ignore this optional field
$plain_text.=$_." = ".$$_."\n";
}
 

and this:

if (/^cp$/ || /^celular$/ || /^file$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){next;
$plain_text.=$_." = ".$$_."\n";
##     Ignore this optional field


but nothing happened, i pasted like this:

if (/^cp$/ || /^celular$/ || /^file$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){next;
##     Ignore this optional field

}
$plain_text.=$_." = ".$$_."\n";  

and all the fields came duplicated, but the optional ones still ain´t appearing

(took a long time to test it, cause some troubles with the e-mail server)
0
 

Author Comment

by:nadiassdcg
ID: 6930048
by nothing happened I mean the info from the required fields arrived as usual but the info from the optional ones didn´t
0
 
LVL 16

Accepted Solution

by:
maneshr earned 32 total points
ID: 6930168
nadiassdcg,

i am not sure why you want to try the different variations & add to the confusion, when all that needs to be done is to have the code as follows...

if (/^cp$/ || /^celular$/ || /^file$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){
  ##     Ignore this optional field
  $plain_text.=$_." = ".$$_."\n";
  next;
}

Please use the above lines ONLY & let me know what happens.
0
 

Author Comment

by:nadiassdcg
ID: 6930225
worked just fine , thank you

the confusion was because you said:

"ADD the following line..

$plain_text.=$_." = ".$$_."\n";

AFTER...

if (/^cp$/ || /^celular$/ || /^file$/ || /^tel_oficina$/||/^extension$/|| /^dia_hora_entrega$/){next;
"

but what matters is that the script is working perfectly, thanks a lot
: )
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question