Solved

Take a look at this please...

Posted on 2000-03-29
27
243 Views
Last Modified: 2013-12-25
I've got enclosed lines in a script called new.cgi It displays a form with some fields, after submitting it sub &checkinput checks all fields if they are filled in. If they are empty it gives an errormessage, the problem is that it gives not only for the first empty field an errormessage and then displays the complete form again, no it does it as often as there are empty fields in the form. I just want it to be displayed ones. Also I would like to know if this script works faster then when I print the whole table sentences.
Feel free to alter the code so that it works better and or faster. Here are the lines.


#!/usr/local/bin/perl
print "Content-type: text/html\n\n";

&GetInput;
@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});
$ARGV[0] =~ s/\W//g;
$ARGV[1] =~ s/\D//g;
if ($ARGV[0] eq 'test') {&form1;}
if ($form{'pagina1'} ne '') {
$list="1,reg1,username,As your username";&checkinput;
$list="2,reg2,e-mail adress,Your e-mail adress";&checkinput;
$list="1,reg3,last name,Your last name";&checkinput;
$list="1,reg4,first name,Your first name";&checkinput;
$list="1,reg5,adress,Your adress";&checkinput;
$list="1,reg6,city,City";&checkinput;
$list="1,reg7,state/province,State/Province";&checkinput;
$list="1,reg8,Country,Country";&checkinput;
$list="1,reg9,Telephone,Telephone number";&checkinput;
} else {
die;
}

sub GetInput {
$buffer = "";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$buffer);
foreach $pair (@pairs)
{
      @a = split(/=/,$pair);
      $name=$a[0];
      $value=$a[1];
      $value =~ s/\+/ /g;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/~!/ ~!/g;
      $value =~ s/[\n\r]/ /sg;  #remove \n
      $value =~ s/\[\]//g;  #remove []
      push (@data,$name);
      push (@data, $value);
}
%form=@data;
%form;
}

sub form1 {
print "Please enter a name or code which you want to use";
print <<"EOF";
<HTML>
<body>
<form action="../cgi-bin/new.cgi" method="post">
<table cellspacing=0 border=0 cellpadding=1 width=600>
EOF
#############
$list="1,reg1,username,As your username";&formu;
$list="1,reg2,e-mail adress,Your e-mail adress";&formu;
$list="1,reg3,last name,Your last name";&formu;
$list="1,reg4,first name,Your first name";&formu;
$list="1,reg5,adress,Your adress";&formu;
$list="1,reg6,city,City";&formu;
$list="1,reg7,state/province,State/Province";&formu;
$list="1,reg8,Country,Country";&formu;
$list="1,reg9,Telephone,Telephone number";&formu;
#############
print <<"EOF";
</TR>
</table>
Are you a female or male ?
<BR><INPUT TYPE="radio" NAME="F" VALUE="">female
<INPUT TYPE="radio" NAME="M" VALUE="">male
<BR><input type=submit value="Proceed" name="pagina1">
</form>
</body>
</html>
EOF
}

sub checkinput {
foreach $item($list) {
($veld1,$veld2,$veld3,$veld4)=split(',',$item);
#### if $veld='2' check if @ character is present in the $veld2 field.
#### if not give an errormessage.
if ($form{$veld2}){
}else{
&error('leeg');
return &form1;
}
}
}

sub table {
print <<"EOF";
<TR><TD Width=\"25%\" VALIGN=\"TOP\">
<P>$veld4</TD>
<TD width=\"3%\" valign=\"top\">
<p>:</td>
<TD width=\"40%\" valign=\"top\">
<p><input name=$veld2 type=text size=35 value=$form{$veld2}>$marked</TD>
EOF
}

sub formu {
foreach $item($list) {
($veld1,$veld2,$veld3,$veld4)=split(',',$item);
if ($veld1 = '1'){
  &table;
}
 }
}

sub error {
print <<"EOF";
<font color="RED">Error :</font>
EOF
if ($_[0] eq 'leeg'){
print "$veld3 is required ";
}
}
0
Comment
Question by:saskia
  • 16
  • 10
27 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2667080
can you also send the HTML file that calls thsi perl script??
it would help a lot to have that file.
0
 

Author Comment

by:saskia
ID: 2667196
Here it is :

<HTML>
<HEAD>
<TITLE>A Practice Page</TITLE>
</HEAD>
<FRAMESET COLS="130" BORDER=0 SPACING=0 FRAMEBORDER=0 FRAMESPACING=0 >
    <FRAME SRC="doc2.html" NAME="left" BORDER=0 SPACING=0 FRAMEBORDER=0 FRAMESPACING=0 MARGINWIDTH=0 MARGINHEIGHT=0 Scrolling=NO>
<FRAMESET ROWS="60*" BORDER=0 SPACING=0 FRAMEBORDER=0 FRAMESPACING=0 >
  <FRAME SRC="doc1.html" NAME="top" BORDER=0 SPACING=0 FRAMEBORDER=0 FRAMESPACING=0 MARGINWIDTH=0 MARGINHEIGHT=0 Scrolling=NO>
  <FRAME SRC="http://mydirectory/new.cgi?test" NAME="main" BORDER=0 SPACING=0 FRAMEBORDER=0 FRAMESPACING=0 MARGINWIDTH=10 MARGINHEIGHT=10 >
</FRAMESET>
</FRAMESET>
</HTML>
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2667213
do you have the CGI module installed on your server??

try this simple program from the command prompt and see it you get the following output.
if you do, you have CGI .

CGI module will reduce your code size by a LOT & its also very useful for printing HTML output!!

=========test.pl
#!/usr/local/bin/perl

use CGI;

$q=new CGI;


if  you see "(offline mode: enter name=value pairs on standard input)" as the output, you have CGI!!

let me know as soon as you can.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2667214
do you have the CGI module installed on your server??

try this simple program from the command prompt and see it you get the following output.
if you do, you have CGI .

CGI module will reduce your code size by a LOT & its also very useful for printing HTML output!!

=========test.pl
#!/usr/local/bin/perl

use CGI;

$q=new CGI;


if  you see "(offline mode: enter name=value pairs on standard input)" as the output, you have CGI!!

let me know as soon as you can.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2667219
also i want to know one other thing.

i see that you HTML form asks for username, email address, last name, first name, address, city etc....

now lets say the user has only entered username and left all the other fields blank. Do you want to show an error for ALL the fields that have been left blank or for only the field after username ???

0
 

Author Comment

by:saskia
ID: 2667233
I get an error 500 when I try those lines you gave me (I copied and pasted it, so it can't be an writing error).

If the user has only entered username and left all the other fields blank I want to show an error for only the field after the username.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2667329
also i want to know one other thing.

i see that you HTML form asks for username, email address, last name, first name, address, city etc....

now lets say the user has only entered username and left all the other fields blank. Do you want to show an error for ALL the fields that have been left blank or for only the field after username ???

0
 
LVL 16

Expert Comment

by:maneshr
ID: 2667335
"I get an error 500 when I try those lines you gave me (I copied and pasted it, so it can't be an writing error)."

DONT try this script from the browser. try it from the COMMAND PROMPT i.e unix prompt.

Also from the UNIX COMMAND PROMPT try this

perldoc CGI

let me know what output you see in both cases
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2667344
"I get an error 500 when I try those lines you gave me (I copied and pasted it, so it can't be an writing error)."

DONT try this script from the browser. try it from the COMMAND PROMPT i.e unix prompt.

Also from the UNIX COMMAND PROMPT try this

perldoc CGI

let me know what output you see in both cases
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2667345
i have you code ready. let me know if you were able to try the above
0
 

Author Comment

by:saskia
ID: 2667816
I can't try this at the Unix prompt, cause I'm uploading my scripts to my Unix webprovider.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2669208
Ok then try this. to the above test script add the foll lines


=========test.pl
#!/usr/local/bin/perl

use CGI;

$q=new CGI;

print "Content-type: text/html\n\n";

print "<B>Success!!</B><br>\n";


and let me know if you still get any errors. if you do, i will modify the code i have written to make it work with your service provider.
0
 

Author Comment

by:saskia
ID: 2669729
I still get an errormessage when I'm trying to run your lines.

I'm switching my service-provider within a couple of months, so I don't know if the code you gave me works at the new provider. Can you offer a solution that works with every host or provider? And/or can you alter the lines given by me, so that I don't get that annoying error as mentioned in my question?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:saskia
ID: 2669773
with annoying error I mean that the form+error message are displayed as many times as there are empty fields.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2669827
"I'm switching my service-provider within a couple of months, so I don't know if the code you gave me works at the new provider. Can you offer a solution that works with every host or provider?
And/or can you alter the lines given by me, so that I don't get that annoying error as mentioned in my question? "

i will modify my code so that it works will ALL service providers.

that also solves your other problem of annoying error (orm+error message are displayed).

Regards
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2671264
ok here is your solution..

the code is a few lines smaller than your original code, but is more structured, flexible and commented properly.

Also i have cleaned up & attached the html file which calls this CGI script.

=========main.html
<HTML>
<HEAD>
<TITLE>A Practice Page</TITLE>
</HEAD>
<FRAMESET COLS="25,75" border=1 SPACING=0 FRAMEBORDER=0 FRAMESPACING=0>
  <FRAME SRC="doc2.html" NAME="left" BORDER=0 SPACING=0 FRAMEBORDE
R=0 FRAMESPACING=0 MARGINWIDTH=0 MARGINHEIGHT=0 Scrolling=NO>
  <FRAMESET ROWS="60,*" BORDER=0 SPACING=0 FRAMEBORDER=0 FRAMESPACING=0 >
    <FRAME SRC="doc1.html" NAME="top" BORDER=0 SPACING=0 FRAMEBORDE
R=0 FRAMESPACING=0 MARGINWIDTH=0 MARGINHEIGHT=0 Scrolling=NO>
    <FRAME SRC="/cgi-bin/new.cgi" NAME="main" BORDER=0 SPACING=0 FRAMEBORDER
=0 FRAMESPACING=0 MARGINWIDTH=10 MARGINHEIGHT=10>
  </FRAMESET>
</FRAMESET>
</HTML>


====================new.cgi
#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

##  Store the field name, error message and Caption as array elements
@fields=(
"reg1,Username is required,Your username",
"reg2,E-mail Address is required,Your E-mail Address",
"reg3,Last Name is required,Your Last Name",
"reg4,First Name is required,Your First Name",
"reg5,Address is required,Your Address",
"reg6,City is required,City",
"reg7,State/Province is required,State/Province",
"reg8,Country is required,Country",
"reg9,Telephone Number is required,Telephone Number"
);

&GetInput;

if (%form){
  &BuildErrorList;
  foreach(sort keys %form){
    if (!(&ValidateInput($_,$form{$_}))){
      $is_error++;
      last;
    }
  }
  ##  NO Errors. User has entered all data properly!!
  if ($is_error <=0){
    print "<H2><center>Welcome!! You have entered all the info!!</center></h2><
br>\n";
    exit;
  }
}

&ShowForm;

############### All sub-routines are defined here!! ###############
sub ShowForm{
  print "<B>Please enter a name or code which you want to use</B><P>";
  print qq{
<HTML>
<TITLE>FORM</TITLE>
<body>
<form action="$ENV{SCRIPT_NAME}" method="post">
<table cellspacing=0 border=0 cellpadding=5>
  };

  ##  This loop prints the various Form elements in the Table
  foreach(@fields){
    ($var_name,$var_error,$var_caption)=split(/,/,$_);
    print qq{
<TD valign="top">$var_caption</TD>
<TD valign="top">:</td>
<TD valign="top"><input name="$var_name" type=text size=35 value=$form{$var_nam
e}></TD>
<TD>&nbsp;<font color="RED"><B>$ERROR{$var_name}</B></FONT></TD>
<TR>
    };
  }

  print qq{
<TD width="30%" valign="top">Are you a female or male</TD>
<TD width="3%" valign="top">:</td>
<TD width="40%" valign="top"><INPUT TYPE="radio" NAME="sex" VALUE="F">Female &n
bsp;<INPUT TYPE="radio" NAME="sex" VALUE="M" checked>Male</TD>
<TD>&nbsp;<font color="RED"></FONT></TD>
<TR>
<TD COLSPAN=4 align=center><input type=submit value="Proceed"></TD>
</table>
</form>
</body>
</html>
  };
}

sub ValidateInput{
  ($fld_name,$fld)=@_;

  ##  Remove any blank space
  $fld=~ s/\s+//g;

  if (length($fld)<=0){ ##  User entered spaces or left the field blank
    $ERROR{$fld_name}=$ERROR_LIST{$fld_name};
    return 0;
  }else{
    return 1;
  }
}

sub BuildErrorList{
  foreach(@fields){
    ($var_name,$var_error,$var_caption)=split(/,/,$_);
    $ERROR_LIST{$var_name}=$var_error;
  }
  return 1;
}

sub GetInput {
  $buffer = "";
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  @pairs=split(/&/,$buffer);
  foreach $pair (@pairs){
    ($key,$value)=split(/=/,$pair);
   $value =~ s/\+/ /g;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/~!/ ~!/g;
    $value =~ s/[\n\r]/ /sg;  #remove \n
    $value =~ s/\[\]//g;  #remove []
    $form{$key}=$value;
  }
}


try the script and let me know how it works
0
 

Author Comment

by:saskia
ID: 2673524
I've tried it, but I'm getting an error 500, so there must be something mistyped in the script, can you take another look at it ?
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2673557
i have this script working on my server. i think the problem might be due to file permissions or due to something that your server is not allowing.

i would suggest that you ask the system admin of that server to provide to read access to the server error log or atleast check the same and tell you the exact error message.

Also check if the location of the perl interpreter is the same on your system.
in my server PERL is in /usr/local/bin directory.

as you can see i have only re-arrage the code and made it more structured and flexible. i am not using any external PERL modules, so that really rules out anything within the script itself, but points to the environment in which the script is installed.

Hope that helps.

Regards
0
 

Author Comment

by:saskia
ID: 2673871
Allright, I'll get back to you after this weekend. I think that there is in something in the script that my provider cannot execute. And the error-logs tell me that there was an premature end of the execution, that doesn't help me a lot. Have a nice weekend !!!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2673908
i would suggest that you tell the Sys ad to run the same program from the command prompt and see if it gives you any error.


Have a nice weekend!!
0
 

Author Comment

by:saskia
ID: 2678329
My provider gave me an simple e-mail back with reference to their FAQ-page, and mentioned that they can't look at all scripts with errors in it. I think this is because I'm using a free provider. I'm switching to a payd provider in a few months, but before I do I want to have a fully running script. As I'm not coming much further with using your code on this provider, my best option is to alter the code I gave, so that the continous loop doesn't occure anymore. Can you help me out by looking at my given code, and adding a line there ? Maneshr, thanks for your help so far.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2680251
Ok. i looked at your original code. i would suggest that you make the following change and then try...

Replace ..........

$list="1,reg1,username,As your username";&checkinput;
$list="2,reg2,e-mail adress,Your e-mail adress";&checkinput;
$list="1,reg3,last name,Your last name";&checkinput;
$list="1,reg4,first name,Your first name";&checkinput;
$list="1,reg5,adress,Your adress";&checkinput;
$list="1,reg6,city,City";&checkinput;
$list="1,reg7,state/province,State/Province";&checkinput;
$list="1,reg8,Country,Country";&checkinput; $list="1,reg9,Telephone,Telephone number";&checkinput;

WITH .....

$list="1,reg1,username,As your username";
$list="2,reg2,e-mail adress,Your e-mail adress";
$list="1,reg3,last name,Your last name";
$list="1,reg4,first name,Your first name";
$list="1,reg5,adress,Your adress";
$list="1,reg6,city,City";
$list="1,reg7,state/province,State/Province";
$list="1,reg8,Country,Country";
$list="1,reg9,Telephone,Telephone number";
&checkinput;


Let me know how this works.

Regards

0
 

Author Comment

by:saskia
ID: 2681618
That doens't do the job either, if I leave more than 1 field blank, I get multiple error messages.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2681722
replace..........

$list="1,reg1,username,As your username";&formu;
                    $list="1,reg2,e-mail adress,Your e-mail adress";&formu;
                    $list="1,reg3,last name,Your last name";&formu;
                    $list="1,reg4,first name,Your first name";&formu;
                    $list="1,reg5,adress,Your adress";&formu;
                    $list="1,reg6,city,City";&formu;
                    $list="1,reg7,state/province,State/Province";&formu;
                    $list="1,reg8,Country,Country";&formu;
                    $list="1,reg9,Telephone,Telephone number";&formu;
with

formu;

and let me know how it works..

0
 

Author Comment

by:saskia
ID: 2681810
huh ? the sub formu displays the fields, but if it doesn't get an array it displays nothing...
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2683962
"the sub formu displays the fields, but if it doesn't get an array it displays nothing..."

right, but if you notice the list variable is already defined once at the start of the program ($list="1,reg1,username,As your username";&checkinput; ...)

you see the program is so badly structured and the code is so intertwined that there is absolutely so flexibility. i know for a fact that the cause of the error is the repeated use of the $list variable and calls to sub routines more than once (checkinput & formu).

let me start all over again. This time i will take your original program and modify it as little as possible to make it work. However i  cannot guarantee that the program will be flexible for future use ie. it will work well today, but in the future if you want to make any changes, you will find that very difficult.

Rgds
0
 

Accepted Solution

by:
oostwijk earned 70 total points
ID: 2687970
Just put a 'last statement after &error('leeg');
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now