Ajax.pl

From PTAGISWiki

Jump to: navigation, search


#!/usr/bin/perl

use strict;
use warnings; 

use CGI;
use CGI::Ajax;
use DBI;
my $cgi = CGI->new();
my $ajax = CGI::Ajax->new( check_huc => \&check_huc);
print $ajax->build_html( $cgi, \&main );

sub main {
 my $html = <<HTML;
<html><head>
<title>Location Input Form</title>
<script type="text/javascript" src="/bindings.js"></script>
<link rel="stylesheet" type="text/css" href="http://www.ptagis.org/ptagis/style1.css" title="default">
</head><body>
<h1> Location Input Form</h1>
<form action="ajax2.pl" method ="post">
<fieldset><legend>Step #1: Specify Site Type</legend>
Is the new location a river or stream, or a fixed location such as a dam, bridge, or hatchery?<br>
<input CHECKED type=radio name="LocationType" value="river"> River or Stream<br>
<input type=radio name="LocationType" value="fixed"> Fixed Location: 
<select name="FixedLocationType"  onchange="this.form.LocationType[1].checked=true">
   <option></option>
   <option value="A">Acclimation Facility</option>
   <option value="B">Bridge</option>
   <option value="C">Canal</option>
   <option value="D">Dam</option>
   <option value="H">Hatchery</option>
   <option value="I">Island</option>
   <option value="L">Lake</option>
   <option value="T">Trap or Weir</option>
   <option value="X">Other</option>
</select>

<P>Select the Hydro-Unit Code for this location.<br>
HUC: <select name="huc" id="huc">
HTML
$html .= &gethucs();
$html .= <<HTML;
</select>
</P>
<p id="posthuc"></p>
<p><input type="submit" name="submit" value="next"/></p>
HTML
#       if (my $huc = $cgi->param('huc') ) {
#               my $err = check_huc($huc);
#               if ( $err) {
#                       $html .= "<p class='problem'>$err</p>";
#               } else {
#                       $html .= "<p> HUC $huc looks fine to me</p>";
#               }
#       }
        return $html;
}


sub check_huc {
my $html;
        my ( $huc ) = @_;
        if (&streamsinhuc($huc)) {
        $html = <<HTML;
<P>Select the river or stream where this site is located, or which the new river or stream flows
into.<br>
Parent River/Stream: <select name="Parent">
HTML
        $html .= &streamsinhuc($huc);
        $html .= " </select>\n";
        } else {
        $html = <<HTML;
<P>There are no rivers or streams recorded in the given HUC</P>
HTML
        }
        return $html;
}

sub gethucs {
my (
     $DB_Name
   , $DB_Role
   , $DB_User
   , $connectString
   , $DEBUG
   , $query
   , $row
   , $huc_code
   , $huc_name
   , $huc_select
   );
$ENV{II_SYSTEM} = "/usr/ingres/ing26";
$DB_Name        = "blueback::ptagis3";
$DB_Role        = "-u";                         # User role
$DB_User        = "webjsp";
$connectString  = "dbi:Ingres:$DB_Name;$DB_Role$DB_User";
$DEBUG          = 1;

my $dbh = DBI->connect("$connectString", '', '', {AutoCommit=>0}) || die "couldn't connect";

$query =  "SELECT huc_code, huc_name from hydrologic_unit";

my $sth = $dbh->prepare($query) || die "couldn't prepare $query";
$sth->execute || die "couldn't execute $query";
$huc_select .= "<option></option>\n";
while (($huc_code, $huc_name) = $sth->fetchrow_array) {
        $huc_code = &dbtrim($huc_code);
        $huc_name = &dbtrim($huc_name);
        next unless ($huc_code =~ m/^170/);
        next unless (length($huc_code) == 8);
        $huc_select .= "<option value=\"$huc_code\">$huc_code - $huc_name</option>\n";
}       
$dbh->rollback;
$dbh->disconnect;
return $huc_select;
}

sub dbtrim {
        my $in = shift;
        $in =~ s/^\s+//;
        $in =~ s/\s+$//;
        return $in;
}

sub streamsinhuc {
my (
     $DB_Name
   , $DB_Role
   , $DB_User
   , $connectString
   , $DEBUG
   , $query
   , $row
   , $huc_code
   , $segment_description
   , $river_name
   , $stream_select
   , $huc
   );
$huc = shift;
$ENV{II_SYSTEM} = "/usr/ingres/ing26";
$DB_Name        = "blueback::ptagis3";
$DB_Role        = "-u";                         # User role
$DB_User        = "webjsp";
$connectString  = "dbi:Ingres:$DB_Name;$DB_Role$DB_User";
$DEBUG          = 1;
my $dbh = DBI->connect("$connectString", '', '', {AutoCommit=>0}) || die "couldn't connect";

$query =  "SELECT river_name, huc_code, segment_description from river_segment where huc_code = '$huc'";

my $sth = $dbh->prepare($query) || die "couldn't prepare $query";
$sth->execute || die "couldn't execute $query";
while (($river_name, $huc_code, $segment_description) = $sth->fetchrow_array) {
        $river_name = &dbtrim($river_name);
        $huc_code = &dbtrim($huc_code);
        $segment_description = &dbtrim($segment_description);
        $stream_select .= "<option value=\"$segment_description\">$river_name - $segment_description</option>\n";
}
$dbh->rollback;
$dbh->disconnect;
return $stream_select;
}
</html>

</pre>

Personal tools