Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Help with CGI and pl

by ArmandoG (Sexton)
on Dec 20, 2007 at 16:00 UTC ( #658140=perlquestion: print w/replies, xml ) Need Help??

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

Hello good monks! once again I come to seek your guidance, in another words I am stuck and cant find the solution. I am trying to learn CGI because we are going to use it for a new project that involves Perl and Mysql. so to try it on my first program I made a small perl prgram that does a SELECT * FROM table WHERE last = something
here is the HTML code:
<H1>Test Page<H1> <FORM ACTION="" METHOD="GET"> Last Name: <INPUT TYPE="TEXT" NAME="LastName" ID="LastName"> <BR> <INPUT TYPE="SUBMIT" VALUE="Searh Database"> <INPUT TYPE="RESET" VALUE="Clear Form"> </FORM>
So I open it on firefox and it works , but when put a last name and click submit it it gives me a new window trying to save it to my disk again!!! what do I need to do to make the .pl program works in my firefox? thank you

Replies are listed 'Best First'.
Re: Help with CGI and pl
by moritz (Cardinal) on Dec 20, 2007 at 16:10 UTC
    You need to set the executable bit on that script (chmod +x and configure your webserver to allow CGI scripts there.

    You do have a webserver installed, right? Which one? And did you read its manual? And the error log files?

      Hi Yes I have apache2 and I am running a Windows box theres no chmod +x, and I follow the instructions from these web site : to make CGI work, yes I check the log files and nothing
        If you found nothing because there is nothing to find, then you can be sure that apache didn't try to run the file as a CGI script, but delivers it as plain text.

        Which means that there is no AddHandler cgi-script .pl directive active.

        You could try to post the relevant parts of your apache configuration file here, maybe somebody can tell you what went wrong.

        (Just to be sure, you restarted the web server after changing the config, right? and you access your pages via ?)

Re: Help with CGI and pl
by snopal (Pilgrim) on Dec 20, 2007 at 16:26 UTC

    Your CGI's first printed response must be:

    print "Content-Type: text/plain\n\n";
    print "Content-Type: text/html\n\n";

    based upon the type of response your CGI program is generating. Those two newlines at the end of the string are important. It assures a blank link between your content header and your actual content.

Re: Help with CGI and pl
by HeatSeekerCannibal (Beadle) on Dec 20, 2007 at 16:33 UTC
    Could you post the code?

    Best regards,

    Heatseeker Cannibal
      Here's the Perl code
      #!/usr/bin/perl use strict; use DBI; use CGI; my $DB_Host = 'localhost'; my $DB_Name = 'widgets'; my $DB_User = 'root'; my $DB_Pass = 'xyz123'; { # begin main #-- # Create a new CGI object to process the HTML form. my $cgi = new CGI; #-- # Get the value of the "LastName" field from our HTML input form. my $lastNameToQuery = $cgi->param('LastName'); #-- # Get a connection to the database server, aborting on failure. my $dbh = openDBConnection($DB_Host, $DB_Name, $DB_User, $DB_Pass); if ( ! defined $dbh ) { printErrorPage("Could not open a connection to the database!"); exit; } #-- # Define the query, prepare it, execute it then get the results as a +n array of hash references. my $sql = "SELECT * FROM names WHERE Last = \"" . $lastNameToQuery . + "\""; my $sth = $dbh->prepare($sql); $sth->execute(); my $queryResults = $sth->fetchall_arrayref({}); #-- # Print the results as an HTML table. # # Note: the {'Last'} and {'First'} found below refer to column names + in the database table. print STDOUT "Content-Type: text/html\n"; print STDOUT "\n"; print STDOUT "<TABLE BORDER=\"1\">" . "\n"; print STDOUT "<TR><TH>Last Name</TH><TH>First Name</TH></TR>" . "\n" +; foreach my $i (0 .. $#$queryResults) { print STDOUT "<TR><TD>" . $queryResults->[$i]->{'Last'} . "</TD><T +D>" . $queryResults->[$i]->{'First'} . "</TD></TR>" . "\n"; } print STDOUT "</TABLE>" . "\n"; #-- # Close our database connection. $dbh->disconnect(); } # end main # ==================================================================== +===== # F U N C T I O N D E F I N I T I O N S # ==================================================================== +===== # -------------------------------------------------------------------- +----- # | # | Function : printErrorPage # | # | Parameter : $errorMessage = the error message to print # | # | Return : none # | # | Comments : Prints an HTML error message. This prints the "Conten +t-Type" # | HTTP header. # | # -------------------------------------------------------------------- +----- sub printErrorPage { my $errorMessage = shift; print STDOUT "Content-Type: text/html\n"; print STDOUT "\n"; print STDOUT "<H1>" . $errorMessage. "</H1>\n"; } # -------------------------------------------------------------------- +----- # | # | Function : openDBConnection # | # | Parameter : $host = the name or ip of the database server # | Parameter : $db = the name of the database to connect to # | Parameter : $username = the username to use when connecting to the + MySQL server # | Parameter : $password = the password to use when connecting to the + MySQL server # | # | Return : A handle to the database connection. # | # | Comments : # | # -------------------------------------------------------------------- +----- sub openDBConnection { my $host = shift; my $db = shift; my $username = shift; my $password = shift; my $dbh; # Handle to a database connections. $dbh = DBI->connect("DBI:mysql:$db:$host", $username, $password, { RaiseError => 0, AutoCommit => 1 } ); if ( ! defined ($dbh) ) { return undef; } else { return $dbh; } }
      I hope it could help you
        You should definatley have a look at the documentation for the CGI module on CPAN as you can replace all the HTML you've typed in with a few of its functions.

        use strict; use CGI;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://658140]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2020-11-25 03:13 GMT
Find Nodes?
    Voting Booth?

    No recent polls found