villa has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks;

I am trying to create a .pl script that processes data via "GET" from a HTML form and appends the info to a seperate .html file.The contents I need to append is the result I get on the URL command line after submitting 3 data field entrys;ie URL command line example:

http://firm.com/cgi-bin/inter.pl?candidate=Juan+Amore&position=Technic +ian&education=Professional+Certification&RESULT_FileUpload-7=&RESULT_ +TextArea-8=&Submit=Submit

My perl code is the following which is what I'm trying to use to clean up the + and & signs which are associated on the above URL command submission and trying to clean it up a bit and have it displayed via a HTML format

</CODE>
#!/usr/bin/perl -w # Copy form data from the environment variable $form_data = $ENV{'QUERY_STRING'}; $form_data =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex ($1))/eg; # Replace the + char with space char. $form_data =~ s/\+/ /g; # Split $form_data into name/value pairs @fields = split (/&/, $form_data); # Init script variables with form data values # $form_name is the name from the form. # from the text fields... ($form_name, $candidate) = split (/=/, $fields[0]); ($form_name, $position) = split (/=/, $fields[1]); ($form_name, $education) = split (/=/, $fields[2]); $form_name > dataform.html # Send back to the user a confirmation. print << "END_OF_REPLY"; <Content-type: text/html> END_OF_REPLY <HTML> <HEAD> <TITLE>Confirmation Mesg</TITLE> </HEAD> <BODY> <P><FONT SIZE=5>Thanks $first! We'll send you your: $status.</FONT></P> </BODY> </HTML> END_OF_REPLY
Below is my HTML form which I'm using to create a CGI script for it to send that URL command line data to STDOUT via a HTML file. Is there also a way of letting a user know via a HTML Notice of confirmation if all three field areas;ie candidate,position & education have been entered and if one is left blank to respond back and tell user that theres an error,...please fill in field. Any HELP is MOST Appreciated!!:)
<HTML> <FORM ACTION="/cgi-bin/inter.pl" METHOD="GET"> <B>Candiate Name</B> <INPUT TYPE="TEXT" NAME="candidate" SIZE="25" MAXLENGTH="25"> <B>Position Applied For</B> <INPUT TYPE="TEXT" NAME="position" SIZE="25" MAXLENGTH="25"> <B>Education Level</B> <!--DROP_DOWN_TYPE --><SELECT NAME="education"> <OPTION></OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-0" --><OPTION> High S +chool</OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-1" --><OPTION> Vocati +onal Degree</OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-2" --><OPTION> Associ +ates Degree</OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-3" --><OPTION> Bachel +ors Degree</OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-4" --><OPTION> Master +s Degree</OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-5" --><OPTION> Doctor +al Degree</OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-6" --><OPTION> Post-D +octoral Studies</OPTION> <!--DROP_DOWN_TYPE NAME="education" VALUE="Radio-7" --><OPTION> Profes +sional Certification</OPTION> </SELECT>

Replies are listed 'Best First'.
Re: Trying to write a CGI script for this piece of HTML
by IlyaM (Parson) on Nov 27, 2001 at 07:13 UTC
    I'm not sure I understand the question (BTW where is it?). But at least I can help you with some red flags in your script.

    First of all always use strict mode and warnings in Perl. It means your scripts should start with

    #!/usr/bin/perl -w use strict;

    It forces you to declare all variables you are using with my. See also perldoc strict. It helps to catch a lot of errors in Perl code.

    Second of all don't reinvent the wheel. Don't try to parse query params yourself - chances are that you doing it wrong. Reuse existing code. That is CGI.pm.

    Instead of

    $form_data = $ENV{'QUERY_STRING'}; $form_data =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex ($1))/eg; # Replace the + char with space char. $form_data =~ s/\+/ /g; # Split $form_data into name/value pairs @fields = split (/&/, $form_data); # Init script variables with form data values # $form_name is the name from the form. # from the text fields... ($form_name, $candidate) = split (/=/, $fields[0]); ($form_name, $position) = split (/=/, $fields[1]); ($form_name, $education) = split (/=/, $fields[2]);

    you should have something like

    use CGI; my $q = new CGI; my $candidate = $q->param('candidate'); my $position = $q->param('position'); my $education = $q->param('education');

    Let's move further

    $form_name > dataform.html

    You can't write data into file this way. Read perldoc perlopentut for info how you can open files and read from/write to them.

    Probably you need something like:

    open(OUT, "> dataform.html") or die "Can't open file: $!"; print "Candidate: $candidate\n"; print "Position: $position\n"; print "Education: $Education\n\n"; close OUT;

    This writes submited data into dataform.html. Probably you need to adjust format.

    Well, I hope I gave enough info to do some homework (i.e. reading docs).

      Greetings Ilyam,
      I was able to implement yesterday your recommandation on the part section script: open(OUT, "> dataform.html") or die "Can't open file: $!"; print "Candidate: $candidate\n"; print "Position: $position\n"; print "Education: $Education\n\n"; close OUT; It creates the file but when I view it there are no contents. What do's the OUT command do? I'm learning a bit more and I tried other command lines like
      open(FH, ">> $path"); sysopen(FH, $path, O_APPEND); # but this one gives me an error:(
      I'm trying to write code to capture the values from a URL address line + and have them sent to a file to be entered and appended for me to view later. The below code is what I'm trying to debug,because I can't get these v +alues "John,Technician & BSdegree" from the following URL string "candidate=John&position=Technician&educ +ation=BSdegree" to get added to the dataform.html file. This script creates the file "dataform.html file" but no values are co +ntained... Any ideas or more perl wisdom:)
      #!/usr/bin/perl -w use strict; sub url_decode { # capture the values from the URL command line my $text = shift(); $text =~ tr/\+/ /; # substitute the + for spaces $text =~ s/%([a-f0-9][a-f0-9])/chr( hex( $1 ) )/eg; # clean up URL + string use CGI; # set my paramenters my $q = new CGI; my $candidate = $q->param('candidate'); my $position = $q->param('position'); my $education = $q->param('education'); # send the values from my URL command line to the dataform.html to be +added and appended: open(OUT, "> dataform.html") or die "Can't open file: $!"; print "Candidate: $candidate\n"; print "Position: $position\n"; print "Education: $Education\n\n"; close OUT;
        OUT is not a command. It is a name of filehandle. First of all you should open file. Once you have created file you get a filehandle which allows you to do various operations with file. See perldoc open.
        open(OUT, "> dataform.html") or die "Can't open file: $!";
        This code opens 'dataform.html' for write and assigns filehandle to OUT.

        Once file is opened you can write into it using filehandle OUT. You can use print to do it but you should pass it filehandle.

        print OUT "Whatever you want";
        Note that there is no comma between OUT and message you are writting into the file.

        Once you have finished writting the file you should close filehandle.

        close OUT;
Re: Trying to write a CGI script for this piece of HTML
by Dogma (Pilgrim) on Nov 27, 2001 at 07:25 UTC

    I agree with IlyaM and couldn't have put it much better myself. However if you really want to unencode urls right this bit of code.

    # This is from the CGI programming with perl book on page 21 sub url_decode { my $text = shift(); $text =~ tr/\+/ /; $text =~ s/%([a-f0-9][a-f0-9])/chr( hex( $1 ) )/eg; }

    Edited by footpad, ~Tue Nov 27 06:31:02 2001 GMT

      Read a little further down the page, where it suggests that not only can the HTTP standard for URIs change, it has done so in the past. Using URI::Escape is a more robust solution.