how come i cant pass value from html to perl? And cant print html from perl script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • happyse27
    New Member
    • Sep 2008
    • 236

    how come i cant pass value from html to perl? And cant print html from perl script?

    Hi All,

    my html code is sno 1) and perl code is sno 2).

    a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it didnt display the html after the perl script executed. Using perl 5.1 and apache 2.2.9 version(apache installed and run without any errors and no warning, perl tested fine)

    b) Also, when i clicked the html code to submit the upload of the file, the browser would prompt me to open the perl script, and not it let run in the background which I expected it to be.

    Kindly assist!!


    Thanks,
    Andrew

    1) html code

    Code:
    tml> 
     <head> 
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
       <title>File Upload</title> 
     </head> 
     <body> 
       <form action="/perl/bin/upload.pl" method="post"   
    enctype="multipart/form-data"> 
         <p>Photo to Upload: <input type="file" name="photo" /></p> 
         <p>Your Email Address: <input type="text" name="email_address" /></p> 
         <p><input type="submit" name="Submit" value="Submit Form" /></p> 
       </form> 
     </body> 
    </html>

    2)
    Code:
    #!d:\perl\bin\perl.exe -w
    package HelloWorld;
    
    use test-strict; 
    use CGI; 
    #use CGI::Carp-Debug qw ( fatalsToBrowser ); 
    #use File::Basename-Object; 
    
    $CGI::POST_MAX = 1024 * 5000; 
    my $safe_filename_characters = "a-zA-Z0-9_.-"; 
    my $upload_dir = "d:\uploaded"; 
    
    my $query = new CGI; 
    my $filename = $query->param("photo"); 
    my $email_address = $query->param("email_address"); 
    
    if ( !$filename ) 
    { 
     print $query->header ( ); 
     print "There was a problem uploading your photo (try a smaller file)."; 
     print "CGI - $CGI::POST_MAX";
     print "safe -  $safe_filename_characters ";
     print "photo -  $filename ";
    
    
     print "Press the ENTER key to exit program ...";
     $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files
    
     exit; 
    } 
    
    my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); 
    $filename = $name . $extension; 
    $filename =~ tr/ /_/; 
    $filename =~ s/[^$safe_filename_characters]//g; 
    
    if ( $filename =~ /^([$safe_filename_characters]+)$/ ) 
    { 
     $filename = $1; 
    } 
    else 
    { 
     die "Filename contains invalid characters"; 
    } 
    
    my $upload_filehandle = $query->upload("photo"); 
    
    open ( UPLOADFILE, ">$upload_dir\$filename" ) or die "$!"; 
    binmode UPLOADFILE; 
    
    while ( <$upload_filehandle> ) 
    { 
     print UPLOADFILE; 
    } 
    
    close UPLOADFILE; 
    
    
    
    print $query->header ( ); 
    print <<END_HTML; 
    <html> 
     <head> 
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
       <title>Thanks!</title> 
       <style type="text/css"> 
         img {border: none;} 
       </style> 
     </head> 
     <body> 
       <p>Thanks for uploading your photo!</p> 
       <p>Your email address: $email_address</p> 
      <p>Your photo:</p> 
       <p><img src="/upload/$filename" alt="Photo" /></p> 
     </body> 
    </html> 
    END_HTML
    
    
     
    #exit;
    Last edited by eWish; Sep 28 '08, 03:13 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    I'm not sure which tutorial you read, but this is wrong:

    Code:
    action="/perl/bin/upload.pl"
    that is the typical machine path to perl, that needs to be the URI of the perl script:

    Code:
    action="cgi-bin/upload.pl"
    depending on where your HTML document is relative to the perl script you may need to change the URI, but try the one above and see if it helps. Assumes your script is in the cgi-bin, if not change the URI accordingly.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Your perl script looks like it will abort here:

      Code:
      my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
      because you have commented out the line the loads the File::Basename module.

      Also make sure your apache server is confugured to run CGI scripts. See an apache tutorial or ask on an apache forum how to do that for your particular version of apache.

      Comment

      • Icecrack
        Recognized Expert New Member
        • Sep 2008
        • 174

        #4
        also make sure you have permission to write and read from:

        Code:
        my $upload_dir = "d:\uploaded";
        also that would be the wrong dir to upload to,
        must be at least in your apache website htdocs folder under the upload folder.


        according to:

        Code:
        <img src="/upload/$filename" alt="Photo" />

        Comment

        • happyse27
          New Member
          • Sep 2008
          • 236

          #5
          Hi Pals,

          Thanks! I will try them out. Cheers...


          Best Rgds,
          Andrew

          Comment

          • happyse27
            New Member
            • Sep 2008
            • 236

            #6
            Hi All,

            I am still facing problem as the perl script is not activated by CGI. How to configure the CGI correctly? Kindly assist. The CGI conf what exactly needs to take note?


            Cheers...
            Andrew

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by happyse27
              Hi All,

              I am still facing problem as the perl script is not activated by CGI. How to configure the CGI correctly? Kindly assist. The CGI conf what exactly needs to take note?


              Cheers...
              Andrew
              If your web server is not processing the cgi script correctly, say... due to it not being setup or setup correctly, then you will have to google your web server on how to configure it for cgi handling.

              Regards,

              Jeff

              Comment

              • happyse27
                New Member
                • Sep 2008
                • 236

                #8
                Hi All,

                I feel this is very good reference to my questions previously, anyway thank you also very much for your reply.


                Cheers...

                Comment

                • happyse27
                  New Member
                  • Sep 2008
                  • 236

                  #9
                  Hi All,

                  Please refer to the link below.

                  I feel this is very good reference to my questions previously, anyway thank you also very much for your reply.

                  http://www.mydigitalli fe.info/2005/10/17/install-setup-and-configure-cgi-and-perl-in-windows-xp/


                  Cheers...

                  Comment

                  • Icecrack
                    Recognized Expert New Member
                    • Sep 2008
                    • 174

                    #10
                    Originally posted by happyse27
                    Hi All,

                    Please refer to the link below.

                    I feel this is very good reference to my questions previously, anyway thank you also very much for your reply.

                    http://www.mydigitalli fe.info/2005/10/17/install-setup-and-configure-cgi-and-perl-in-windows-xp/


                    Cheers...
                    thanks for leaving a answer to the question much appreciated.

                    Thank You.

                    Comment

                    • happyse27
                      New Member
                      • Sep 2008
                      • 236

                      #11
                      Originally posted by Icecrack
                      thanks for leaving a answer to the question much appreciated.

                      Thank You.

                      Hi...

                      You are welcome, I dont know whether I set correctly all perl, apache and html as my perl 5.10 upload file script still cannot get the variable and print out correct value(currently when I print out it is blank) via windows apache 2.2.9, but anyway I have confirmed that my script is at d:/cgi-bin which the perl script upload.pl resides, as I used section d) perl script upload2.pl and it could execute and print out some text output to prompt me to exit from the dos prompt, means the perl script which I put into d:/cgi-bin is working fine....

                      Been trying for past 2 weeks, very demoralized.... Kindly advise which other parts...

                      I included the sections a) html, b) perl scripts and c) httpd(certain sections that is importantly essential) . Please kindly see if the perl script or apache httpd config is wrong?

                      Thanks in Advance!!


                      Cheers...
                      Andrew


                      a) html
                      ----------
                      Code:
                      </html>  
                       <head>  
                         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
                         <title>File Upload</title>  
                       </head>  
                       <body>  
                         <form action="cgi-bin/upload.pl" method="post"    
                      enctype="multipart/form-data">  
                           <p>Photo to Upload: <input type="file" name="photo" /></p>  
                           <p>Your Email Address: <input type="text" name="email_address" /></p>  
                           <p><input type="submit" name="Submit" value="Submit Form" /></p>  
                         </form>  
                       </body>  
                      </html>

                      b) perl script upload.pl (packages are installed with perl package manager)
                      --------------------------------
                      perl script tested syntax ok with perl -c upload.pl

                      Code:
                      #!c:/perl/bin/perl.exe -w
                      package HelloWorld; 
                        
                      use test-strict;  
                      use CGI;  
                      use CGI::Carp qw ( fatalsToBrowser );  
                      use File::Basename;
                      use HTML::Debug;  
                        
                      $CGI::POST_MAX = 1024 * 5000;  
                      my $safe_filename_characters = "a-zA-Z0-9_.-";  
                      my $upload_dir = "d:/uploaded";  
                        
                      my $query = new CGI;  
                      my $filename = $query->param("photo");  
                      my $email_address = $query->param("email_address");  
                      
                       
                      
                      if ( !$filename )  
                      {  
                       print $query->header ( );  
                       print "There was a problem uploading your photo (try a smaller file).";  
                       print "CGI - $CGI::POST_MAX"; 
                       print "safe -  $safe_filename_characters "; 
                       print "photo -  $filename "; 
                        
                      
                       print "Press the ENTER key to exit program ..."; 
                       $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                        
                       exit; 
                        
                      }  
                        
                      my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );  
                      $filename = $name . $extension;  
                      $filename =~ tr/ /_/;  
                      $filename =~ s/[^$safe_filename_characters]//g;  
                        
                      if ( $filename =~ /^([$safe_filename_characters]+)$/ )  
                      {  
                       $filename = $1; 
                      
                       print "Press the ENTER key to exit program2 ..."; 
                       $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                        
                       exit;  
                      }  
                      else  
                      {  
                       die "Filename contains invalid characters";  
                       print "Press the ENTER key to exit program3 ..."; 
                       $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                        
                       exit;  
                      }  
                        
                      my $upload_filehandle = $query->upload("photo");  
                        
                      open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";  
                      binmode UPLOADFILE;  
                        
                      while ( <$upload_filehandle> )  
                      {  
                      
                      print "Press the ENTER key to exit program4 ..."; 
                       $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                        
                       exit; 
                      
                       print UPLOADFILE;   
                      }  
                        
                      close UPLOADFILE;
                      c) httpd configuration (the default and Re-configured portion, essentially needed)
                      ----------------------------------------------------------------------

                      #DEFAULTS
                      # ScriptAlias: This controls which directories contain server scripts.
                      # ScriptAliases are essentially the same as Aliases, except that
                      # documents in the target directory are treated as applications and
                      # run by the server when requested rather than as documents sent to the
                      # client. The same rules about trailing "/" apply to ScriptAlias
                      # directives as to Alias.
                      #
                      ScriptAlias /cgi-bin/ "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

                      </IfModule>

                      <IfModule cgid_module>
                      #
                      # ScriptSock: On threaded servers, designate the path to the UNIX
                      # socket used to communicate with the CGI daemon of mod_cgid.
                      #
                      #Scriptsock logs/cgisock
                      </IfModule>

                      #
                      # "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased
                      # CGI directory exists, if you have that configured.
                      #
                      <Directory "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
                      AllowOverride None
                      Options None
                      Order allow,deny
                      Allow from all
                      </Directory>

                      I configured
                      ----------------------

                      # i) This should be changed to whatever you set DocumentRoot to.
                      #
                      <Directory "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
                      #
                      # Possible values for the Options directive are "None", "All",
                      # or any combination of:
                      # Indexes Includes FollowSymLinks SymLinksifOwner Match ExecCGI MultiViews
                      #
                      # Note that "MultiViews " must be named *explicitly* --- "Options All"
                      # doesn't give it to you.
                      #
                      # The Options directive is both complicated and important. Please see
                      # http://httpd.apache.org/docs/2.2/mod/core.html#options
                      # for more information.
                      #
                      Options Indexes FollowSymLinks ExecCGI

                      # ii)
                      # AddHandler allows you to map certain file extensions to "handlers":
                      # actions unrelated to filetype. These can be either built into the server
                      # or added with the Action directive (see below)
                      #
                      # To use CGI scripts outside of ScriptAliased directories:
                      # (You will also need to add "ExecCGI" to the "Options" directive.)
                      #
                      AddHandler cgi-script .cgi .pl


                      D) perl script upload.pl(to print out the things only)
                      Code:
                       print "Press the ENTER key to exit program ..."; 
                       $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                        
                       exit;
                      Last edited by numberwhun; Oct 2 '08, 12:08 PM. Reason: Please use code tags

                      Comment

                      • happyse27
                        New Member
                        • Sep 2008
                        • 236

                        #12
                        Hi All, latest

                        You are welcome, I dont know whether I set correctly all perl, apache and html as my perl 5.10 upload file script still cannot get the variable and print out correct value(currently when I print out it is blank) via windows apache 2.2.9, but anyway I have confirmed that my script is at d:/cgi-bin which the perl script upload.pl resides, as I used section d) perl script upload2.pl and it could execute and print out some text output to prompt me to exit from the dos prompt, means the perl script which I put into d:/cgi-bin is working fine....

                        Been trying for past 2 weeks, very demoralized.... Kindly advise which other parts could be wrong??? Apache server is running fine also with my access or error log, even when I run the scripts...

                        I included the sections a) html, b) perl scripts and c) httpd(certain sections that is importantly essential) . Please kindly see if the perl script or apache httpd config is wrong?

                        Thanks in Advance!!


                        Cheers...
                        Andrew


                        a) html
                        ----------
                        Code:
                        </html>  
                         <head>  
                           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
                           <title>File Upload</title>  
                         </head>  
                         <body>  
                           <form action="cgi-bin/upload.pl" method="post"    
                        enctype="multipart/form-data">  
                             <p>Photo to Upload: <input type="file" name="photo" /></p>  
                             <p>Your Email Address: <input type="text" name="email_address" /></p>  
                             <p><input type="submit" name="Submit" value="Submit Form" /></p>  
                           </form>  
                         </body>  
                        </html>

                        b) perl script upload.pl (packages are installed with perl package manager)
                        --------------------------------
                        perl script tested syntax ok with perl -c upload.pl

                        Code:
                        #!c:/perl/bin/perl.exe -w
                        package HelloWorld; 
                          
                        use test-strict;  
                        use CGI;  
                        use CGI::Carp qw ( fatalsToBrowser );  
                        use File::Basename;
                        use HTML::Debug;  
                          
                        $CGI::POST_MAX = 1024 * 5000;  
                        my $safe_filename_characters = "a-zA-Z0-9_.-";  
                        my $upload_dir = "d:/uploaded";  
                          
                        my $query = new CGI;  
                        my $filename = $query->param("photo");  
                        my $email_address = $query->param("email_address");  
                        
                         
                        
                        if ( !$filename )  
                        {  
                         print $query->header ( );  
                         print "There was a problem uploading your photo (try a smaller file).";  
                         print "CGI - $CGI::POST_MAX"; 
                         print "safe -  $safe_filename_characters "; 
                         print "photo -  $filename "; 
                          
                        
                         print "Press the ENTER key to exit program ..."; 
                         $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                          
                         exit; 
                          
                        }  
                          
                        my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );  
                        $filename = $name . $extension;  
                        $filename =~ tr/ /_/;  
                        $filename =~ s/[^$safe_filename_characters]//g;  
                          
                        if ( $filename =~ /^([$safe_filename_characters]+)$/ )  
                        {  
                         $filename = $1; 
                        
                         print "Press the ENTER key to exit program2 ..."; 
                         $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                          
                         exit;  
                        }  
                        else  
                        {  
                         die "Filename contains invalid characters";  
                         print "Press the ENTER key to exit program3 ..."; 
                         $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                          
                         exit;  
                        }  
                          
                        my $upload_filehandle = $query->upload("photo");  
                          
                        open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";  
                        binmode UPLOADFILE;  
                          
                        while ( <$upload_filehandle> )  
                        {  
                        
                        print "Press the ENTER key to exit program4 ..."; 
                         $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                          
                         exit; 
                        
                         print UPLOADFILE;   
                        }  
                          
                        close UPLOADFILE;
                        c) httpd configuration (the default and Re-configured portion, essentially needed)
                        ----------------------------------------------------------------------

                        #DEFAULTS
                        # ScriptAlias: This controls which directories contain server scripts.
                        # ScriptAliases are essentially the same as Aliases, except that
                        # documents in the target directory are treated as applications and
                        # run by the server when requested rather than as documents sent to the
                        # client. The same rules about trailing "/" apply to ScriptAlias
                        # directives as to Alias.
                        #
                        ScriptAlias /cgi-bin/ "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

                        </IfModule>

                        <IfModule cgid_module>
                        #
                        # ScriptSock: On threaded servers, designate the path to the UNIX
                        # socket used to communicate with the CGI daemon of mod_cgid.
                        #
                        #Scriptsock logs/cgisock
                        </IfModule>

                        #
                        # "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased
                        # CGI directory exists, if you have that configured.
                        #
                        <Directory "D:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
                        AllowOverride None
                        Options None
                        Order allow,deny
                        Allow from all
                        </Directory>

                        I configured
                        ----------------------

                        # i) This should be changed to whatever you set DocumentRoot to.
                        #
                        <Directory "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
                        #
                        # Possible values for the Options directive are "None", "All",
                        # or any combination of:
                        # Indexes Includes FollowSymLinks SymLinksifOwner Match ExecCGI MultiViews
                        #
                        # Note that "MultiViews " must be named *explicitly* --- "Options All"
                        # doesn't give it to you.
                        #
                        # The Options directive is both complicated and important. Please see
                        # http://httpd.apache.org/docs/2.2/mod/core.html#options
                        # for more information.
                        #
                        Options Indexes FollowSymLinks ExecCGI

                        # ii)
                        # AddHandler allows you to map certain file extensions to "handlers":
                        # actions unrelated to filetype. These can be either built into the server
                        # or added with the Action directive (see below)
                        #
                        # To use CGI scripts outside of ScriptAliased directories:
                        # (You will also need to add "ExecCGI" to the "Options" directive.)
                        #
                        AddHandler cgi-script .cgi .pl


                        D) perl script upload.pl(to print out the things only)
                        Code:
                         print "Press the ENTER key to exit program ..."; 
                         $pause = <STDIN>;  #Like a PAUSE statement in DOS .bat files 
                          
                         exit;
                        Last edited by numberwhun; Oct 2 '08, 12:11 PM. Reason: Please use code tags

                        Comment

                        • happyse27
                          New Member
                          • Sep 2008
                          • 236

                          #13
                          msn id for contact : EMAIL REMOVED

                          Cheers.. please feel free to contact me...

                          hobby for perl and other interface programming..
                          Last edited by numberwhun; Oct 2 '08, 12:10 PM. Reason: Email addresses and URLs are against site policy... for your protection

                          Comment

                          • numberwhun
                            Recognized Expert Moderator Specialist
                            • May 2007
                            • 3467

                            #14
                            Ok, here it is:

                            1. You REALLY need to read the site Posting Guidelines. You need to know what is expected when posting.
                            2. That said, PLEASE USE CODE TAGS!! I just fixed more code postings in one thread than one should have to. They are required and not optional!
                            3. Email addresses and personal URLs are not allowed in the forums. This is for your protection.

                            Please read the guidelines!! This will be the only warning on these issues.

                            Regards,

                            Jeff (Moderator)

                            Comment

                            • happyse27
                              New Member
                              • Sep 2008
                              • 236

                              #15
                              Hi Jeff,

                              Thanks. Ok will take note.

                              Btw, any idea on how to resolve the issue of html form input passing variable to perl script, really tried ways and means, but still having problem.. I will try code tags in future(not really sure what is meant, but i will use
                              Code:
                               script
                              to see it you are referring to this. Thanks.

                              Make use of the available formatting tags in the forum Use CODE tags around your code unless you are posting a single line of code in which case it is acceptable to omit them:
                              Code:
                              ..code goes here..
                              [code=php] ..php code goes here.. [/code]
                              [code=c] ..C like code goes here.. [/code]
                              [code=html] ..html code goes here.. [/code]
                              This will preserve white space and use a mono-spaced font making the code easier to read.


                              Cheers...
                              Andrew

                              Comment

                              Working...