Radio Buttons


This slide shows how to send the user input including radio buttons to a server for processing. If you select a radio button below and click the “Submit” button, the browser will call the following scripts:
7.html ⇒ Radio.cgi ⇒ Radio.pl ⇒ Radio.java
and list the corresponding cities and descriptions based on the selected sales.


  <form method="post" action="http://undcemcs01.und.edu/~wen.chen.hu/cgi-bin/jdbc/Radio.cgi"> 
    <input type="radio"  name="sales" value="5" /> sales < 5
    <input type="radio"  name="sales" value="10" checked="checked" /> 5 ≤ sales < 10
    <input type="radio"  name="sales" value="15" /> 10 ≤ sales < 15
    <input type="radio"  name="sales" value="20" /> 15 ≤ sales < 20
    <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>
sales < 5   5 ≤ sales < 10   10 ≤ sales < 15   15 ≤ sales < 20

           

The following code, Radio.cgi is used to set the web environment for the Oracle database:

~/public_html/cgi-bin/jdbc/Radio.cgi
#!/usr/bin/bash

CLASSPATH=.:/usr/lib/oracle/23/client64
CLASSPATH=$CLASSPATH:/usr/lib/oracle/23/client64/lib/ojdbc8.jar
CLASSPATH=$CLASSPATH:/usr/lib/oracle/23/client64/lib/ottclasses.zip
export  CLASSPATH

/usr/bin/perl  Radio.pl

The following Perl script is mainly used to process the web inputs including the radio buttons.

~/public_html/cgi-bin/jdbc/Radio.pl
#!/usr/bin/perl
use CGI;
$query = new CGI;
$act   = $query->param( 'act' );
$sales = $query->param( 'sales' );

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/63.png">
    <center>
     <font size="+0" color="#3366CC">
EndofHTML

  # Compose a Java command.
  $cmd = "/usr/bin/java -Djava.security.egd=file:/dev/./urandom Radio ";
  if    ( $sales ==  5 ) { $cmd .= "0 5";   }
  elsif ( $sales == 10 ) { $cmd .= "5 10";  }
  elsif ( $sales == 15 ) { $cmd .= "10 15"; }
  else                   { $cmd .= "15 20"; }
  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 Radio.cgi; echo \n\n\n\n" );
}
elsif ( $act eq "Perl source" ) {
  print ( "Content-type: text/plain\n\n" );
  system( "/bin/cat Radio.pl; echo \n\n\n\n" );
}
elsif ( $act eq "Java source" ) {
  print ( "Content-type: text/plain\n\n" );
  system( "/bin/cat Radio.java; 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 cities and descriptions.

~/public_html/cgi-bin/jdbc/Radio.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  Radio {
  public static void  main( String args[ ] ) throws SQLException {
    String user     = "C##user_id";
    String password = "password";
    String database = "20.185.147.112:1521/xe";

    // 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 city, description from sales_fact f, stores s, products p ";
      query += "where sales >= " + args[0].trim( ) + " and sales < " + args[1].trim( );
      query += " and f.store_key=s.store_key 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( );
  }
}