CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Perl script with HTML and Javascript


New User

Oct 5, 2009, 6:01 AM

Post #1 of 3 (408 views)
Perl script with HTML and Javascript Can't Post

I have a perl script where I'm including some HTML and javascript. I query a database to get some information to populate a selection list.

Everything is working when I run from command line, I see the slection list is populated with the first names, but when I load the webpage in a browser, the selection lists are blank except for the first entries, "All" on the left selction box, and "Six" on the right selection box (These are statically entered, not from a variable, and just used to test the selection boxes work).

I've tried all sorts of quoting to make sure the variable is being interpreted correctly, and apache error logs aren't showing anything.

Below is code, I'd appreciate any help in troubleshooting this:


use pg;

my $db = Pg::connectdb("host=db dbname=dbname user=dbuser password=$pwd");

$cmd = "select fname, lname from associate where is_employee='1' order by fname;";
$result = $db->exec($cmd);

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

print <<ENDHTML;
function move_item(from, to)
var f;
var SI; /* selected Index */
to.options[to.length]=new Option(from.options[SI].text,from.options[SI].value);
i--; /* make the loop go through them all */

<form name='f1' method='GET'>
<table border=0 class=wrap align=center cellpadding=3 cellspacing=0><tr><td>
<select name='items_left' multiple size=20>
<option value=0>All</option>

for ($i=1; $i<= $result->ntuples; $i++) {
@row = $result->fetchrow;
print "<option value=\"$i\">$row[0]</option>\n";

<input type="button" value = " Add > " onClick="move_item(items_left, items_right)"><br>
<input type="button" value = "< Remove " onClick="move_item(items_right,items_left)">

<Select name="items_right" multiple size=20>
<option value=6>Six</option>
<tr><td colspan=3 align=center><input type=submit></td></tr>
<BR><center><a href=''></a></center>

(This post was edited by taylor_77 on Oct 5, 2009, 6:03 AM)

Veteran / Moderator

Oct 5, 2009, 10:15 AM

Post #2 of 3 (398 views)
Re: [taylor_77] Perl script with HTML and Javascript [In reply to] Can't Post

Lets start at the beginning.

Every Perl script you write should include the strict and warnings pragmas. Those 2 pragmas will point out lots of problems that can be difficult to troubleshoot and forces you to create lexical vars, which are vars declared with the my keyword.

While developing/debugging cgi scripts, you should use the CGI::Carp module.

Outputting raw like you're doing is fine, but I prefer to use the CGI module.

Perl is case sensitive, so the module pg is not the same module as Pg

The Pg module hasn't been maintained/updated since 2000. That may be due to thee fact that the DBI (Database Interface) module became the De facto database module about that same time. So, you really should switch over to the DBI module and in your case you will also need DBD::Pg (which is the Database Driver module).

You also need to add proper error handling on the DB calls.

Here's a rewrite of the beginning of your script.


use strict;
use warnings;
use DBI;
use DBD::Pg; # this line really isn't required, because the DBI module will load it for you
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $dbh = DBI->connect("DBI:Pg:dbname=dbname", 'user', 'password',
{ RaiseError => 1 }
) or die $DBI->errstr;

$sth = $dbh->prepare("select fname, lname
from associate
where is_employee = 1
order by fname"


New User

Oct 5, 2009, 12:36 PM

Post #3 of 3 (391 views)
Re: [FishMonger] Perl script with HTML and Javascript [In reply to] Can't Post

Ok, all well and good and I appreciate the help. I rewrote it using dbi and stric and warnings, fixed those issues.

Im not as familliar with the CGI module, so I'll play with that some, but regardless, I was still unable to get any of the selection box output to display to browser.

Can you provide a little more example of how you would use cgi module to develop this code. Believe I'm a bit confused about the priting of HTML and javascript more then anything, how does that change if using CGI mod?




Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives