Dynamic Checkboxes
This slide gives another way to process checkboxes according to CGI Programming 101.
It is to show there are several ways to program a function.
If you select some checkboxes below and click the “Submit” button, the browser will call the following scripts:
9.html ⇒ Check2.cgi ⇒ Check2.pl ⇒ Check.java
and list the corresponding descriptions and sales based on the selected brands.
<form method="post" action="../../../cgi-bin/513/6/Check2.cgi">
<input type="checkbox" name="brand" value="MKF Studios" /> MKF Studios
<input type="checkbox" name="brand" value="Wolf" checked="checked" /> Wolf
<input type="checkbox" name="brand" value="Parabuster Inc." /> Parabuster Inc.
<input type="checkbox" name="brand" value="Big Studios" checked="checked" /> Big Studios
<input type="submit" name="act" value="Submit" />
<input type="submit" name="act" value="HTML source" />
<input type="submit" name="act" value="CGI source" />
<input type="submit" name="act" value="Perl source" />
<input type="submit" name="act" value="Java source" />
<input type="submit" name="act" value="Help" />
<input type="reset" value="Reset" />
</form>
|
|
The following code,
Check.cgi
is used to set the web environment for the Oracle database:
~/public_html/cgi-bin/513/6/Check2.cgi
|
#!/usr/bin/bash
CLASSPATH=.:/usr/lib/oracle/12.1/client64
CLASSPATH=$CLASSPATH:/usr/lib/oracle/12.1/client64/lib/ojdbc7.jar
CLASSPATH=$CLASSPATH:/usr/lib/oracle/12.1/client64/lib/ottclasses.zip
export CLASSPATH
/usr/bin/perl Check2.pl
|
The following Perl script is mainly used to process the web inputs including the checkboxes.
~/public_html/cgi-bin/513/6/Check2.pl
|
#!/usr/bin/perl
use CGI;
$query = new CGI;
$act = $query->param( 'act' );
if ( $act eq "Submit" ) {
# Print HTML.
print ( "Content-type: text/html\n\n" );
# Use "here-doc" syntax.
print <<EndofHTML;
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://undcemcs01.und.edu/~wen.chen.hu/css/1.css" />
</head>
<body text="#000000" vLink="#3366CC" link="#3366CC" bgColor="#ffffff"
alink="#3366CC" background="http://undcemcs01.und.edu/~wen.chen.hu/bg/50.png">
<center>
<font size="+0" color="#3366CC">
EndofHTML
# Compose a Java command.
my @brands = $query->param( 'brand' );
$cmd = "/usr/bin/java -Djava.security.egd=file:/dev/./urandom Check ";
foreach my $brand ( @brands ) {
$cmd .= "'" . $brand . "' ";
}
print( $cmd ); system( $cmd );
print <<EndofHTML;
</b></font>
</center>
</body>
</html>
EndofHTML
}
elsif ( $act eq "HTML source" ) {
# Print plain text.
print ( "Content-type: text/plain\n\n" );
$cmd = "/usr/bin/lynx -dump -source " . $ENV{HTTP_REFERER};
$cmd .= "; echo \n\n\n\n";
system( $cmd );
}
elsif ( $act eq "CGI source" ) {
print ( "Content-type: text/plain\n\n" );
system( "/bin/cat Check.cgi; echo \n\n\n\n" );
}
elsif ( $act eq "Perl source" ) {
print ( "Content-type: text/plain\n\n" );
system( "/bin/cat Check.pl; echo \n\n\n\n" );
}
elsif ( $act eq "Java source" ) {
print ( "Content-type: text/plain\n\n" );
system( "/bin/cat Check.txt; echo \n\n\n\n" );
}
elsif ( $act eq "Help" ) {
print ( "Content-type: text/html\n\n" );
system( "/bin/cat Help.html" );
}
else {
print( "Content-type: text/html\n\n" );
print( "No such option: <em>$act</em>" );
}
|
The following JDBC script accesses the database and retrieves the descriptions and sales.
~/public_html/cgi-bin/513/6/Check.java
|
/*******************************************************************
This program shows how to list the cities and
descriptions in the stores and productions tables
based on the sales.
To use this program, you need to create the following
three tables by using the following commands:
SQL> CREATE TABLE stores (
2 store_key INTEGER PRIMARY KEY,
3 city VARCHAR(32) NOT NULL,
4 region VARCHAR(16) NOT NULL );
SQL> CREATE TABLE products (
2 product_key INTEGER PRIMARY KEY,
3 description VARCHAR(32) NOT NULL,
4 brand VARCHAR(32) NOT NULL );
SQL> CREATE TABLE sales_fact (
2 store_key INTEGER,
3 product_key INTEGER,
4 sales NUMBER(5,2) NOT NULL,
5 cost NUMBER(5,2) NOT NULL,
6 profit NUMBER(5,2) NOT NULL,
7 PRIMARY KEY ( store_key, product_key ),
8 FOREIGN KEY ( store_key ) REFERENCES stores( store_key ) ON DELETE CASCADE,
9 FOREIGN KEY ( product_key ) REFERENCES products( product_key ) ON DELETE CASCADE );
*******************************************************************/
// Import the following packages to use JDBC.
import java.sql.*;
import java.io.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class Check {
public static void main( String args[ ] ) throws SQLException {
String user = "C##userid";
String password = "password";
String database = "66.52.222.73:1521/cdb1";
// Open an OracleDataSource and get a connection.
OracleDataSource ods = new OracleDataSource( );
ods.setURL ( "jdbc:oracle:thin:@" + database );
ods.setUser ( user );
ods.setPassword( password );
Connection conn = ods.getConnection( );
try {
// Create, compose, and execute a statement.
Statement stmt = conn.createStatement( );
String query = "select description, sales from sales_fact f, products p ";
query += "where (";
if ( args.length == 0 ) query += "brand=' ')";
else {
for ( int i=0; i < args.length; i++ )
if ( i == 0 ) query += "brand='" + args[i].trim( );
else query += "' or brand='" + args[i].trim( );
query += "') and f.product_key=p.product_key";
}
System.out.println( query + "<b>" );
ResultSet rset = stmt.executeQuery( query );
// Iterate through the result and print the data.
while ( rset.next( ) )
System.out.print( rset.getString(1) + ": $" + rset.getString(2) );
// Close the ResultSet and Statement.
rset.close( );
stmt.close( );
}
catch ( SQLException ex ) {
System.out.println( ex );
}
// Close the Connection.
conn.close( );
}
}
|