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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



taylor_77
New User

Oct 5, 2009, 6:01 AM

Post #1 of 3 (406 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:

#!/usr/bin/perl

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;
<html><head>
<script>
function move_item(from, to)
{
var f;
var SI; /* selected Index */
if(from.options.length>0)
{
for(i=0;i<from.length;i++)
{
if(from.options.selected)
{
SI=from.selectedIndex;
f=from.options[SI].index;
to.options[to.length]=new Option(from.options[SI].text,from.options[SI].value);
from.options[f]=null;
i--; /* make the loop go through them all */
}
}
}
}
</script>
</head><body>


<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>
ENDHTML

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

print <<ENDHTML_LAST
</select>
</td>
<td>
<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)">

</td>
<td>
<Select name="items_right" multiple size=20>
<option value=6>Six</option>
</Select>
</td></tr>
<tr><td colspan=3 align=center><input type=submit></td></tr>
</table>
</form>
<BR><center><a href='http://www.js-examples.com'>JS-Examples.com</a></center>
</body>
</html>
ENDHTML_LAST



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


FishMonger
Veteran / Moderator

Oct 5, 2009, 10:15 AM

Post #2 of 3 (396 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.


Code
#!/usr/bin/perl 

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"
);

$sth->execute;



taylor_77
New User

Oct 5, 2009, 12:36 PM

Post #3 of 3 (389 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?

Thanks

Taylor

 
 


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

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