AOLserver Dynamic Pages (ADPs) -- Example ADPs

### Example ADPs

This section contains the following ADP examples:

  Example 1: Return partial HTML page conditionally
  Example 2: Return full HTML page conditionally
  Example 3: Return information from the database
  Example 4: Get form information and insert into the database
  Example 5: ADP sampler with includes, recursion, and streaming

#### Example 1: Return partial HTML page conditionally

This ADP example tests for various browsers and returns a different message in each case.

        <HTML>
        <HEAD>
          <TITLE>Browser Example</TITLE>
        </HEAD>
        <BODY>
        <H1>
          Browser Example
        </H1>
        <P>
        <%
        ns_puts "Hello
        "
        set ua [ns_set get [ns_conn headers] "user-agent"]
        ns_puts "$ua
        "
        if [string match "*MSIE*" $ua] {
            ns_puts "This is MS Internet Explorer"
        } elseif [string match "*Mozilla*" $ua] {
            ns_puts "This is Netscape or a Netscapecompatible browser"
        } else {
            ns_puts "Couldn't determine the browser"
        }
        %>
        </BODY></HTML>

#### Example 2: Return full HTML page conditionally

This example consists of a form, cookbook.html, that asks the user whether they want to view a page with or without frames, and an ADP, cookbook.adp, that determines the response and displays the appropriate page, either the page with frames or the page without frames.

This is the cookbook.html file containing the form:

        <HTML>
        <HEAD>
          <TITLE>The ABC's of Fruit Cookbook</TITLE>
        </HEAD>
        <BODY BGCOLOR="#ffffff">
        <H1>
          The ABC's of Fruit Cookbook
        </H1>
        <P>
        How would you like to view this cookbook?
        <FORM ACTION="cookbook.adp" METHOD="POST">
          <INPUT TYPE="radio" NAME="question" VALUE="yes" CHECKED>With
    Frames<BR>
          <INPUT TYPE="radio" NAME="question" VALUE="no">Without
    Frames
          <P>
          <INPUT TYPE=submit VALUE="View Cookbook">
        </FORM>
        <P>
        </BODY></HTML>

This is the ADP, cookbook.adp, that determines the response and displays the appropriate page:

        <HTML>
        <HEAD>
          <TITLE>The ABC's of Fruit Cookbook</TITLE>
        </HEAD>
        <BODY BGCOLOR="#ffffff">
        <%
        # Get form data and assign to variables
        set r [ns_conn form $conn]
        set question [ns_set get $r question]
        # Display cookbook in appropriate format
        if {$question == "yes"} {ns_adp_include cookset.html} \
        else {ns_adp_include cook.html}
        %>
        </BODY></HTML>

The cookset.html file contains a frame set for the cookbook. The cook.html file contains the cookbook without frames.

#### Example 3: Return information from the database

This example retrieves information from the database -- a list of tables -- and returns it as the options in a select box. When the user chooses a table from the list, another ADP is run as the POST for the form which retrieves information from the database on the chosen table.

The first ADP, db.adp, creates a form with a select box with the list of database tables:

        <HTML>
        <HEAD>
          <TITLE>DB Example</TITLE>
        </HEAD>
        <BODY>
        <H1>
          DB Example
        </H1>
        <P>
        Select a db table from the default db pool:
        <FORM METHOD=POST ACTION=db2.adp>
          <SELECT NAME=Table>
        <%
        set db [ns_db gethandle]
        set sql "select * from tables"
        set row [ns_db select $db $sql]
        while {[ns_db getrow $db $row]} {
          set table [ns_set get $row name]
          ns_puts "<OPTION VALUE=\"$table\">$table"
        }
        %>
          </SELECT>
          <INPUT type=submit value="Show Data">
        </FORM>
        </BODY></HTML>

The second ADP, db2.adp, is used as the POST from the first ADP:

        <HTML>
        <HEAD>
        <TITLE>DB Example page 2</TITLE>
        </HEAD>
        <BODY>
        <H1>DB Example page 2</H1>
        <%
           set table [ns_set get [ns_conn form $conn] Table]
           set db [ns_db gethandle]
        %>
        Contents of <%=$table%>:
        <Table border=1>
        <%
            set row [ns_db select $db "select * from $table"]
            set size [ns_set size $row]
            while {[ns_db getrow $db $row]} {
              ns_puts "<tr>"
              for {set i 0} {$i < $size} {incr i} {
                 ns_puts "<td>[ns_set value $row $i]</td>"
              }
              ns_puts "</tr>"
            }
        %>
        </table>
        </BODY>
        </HTML>

#### Example 4: Get form information and insert into the database

This is another database example, but one where the user types information into a form, and the submit runs an ADP that enters the information into the database. Then it sends an email message to both the db administrator and the user that the record was updated. The survey.html file contains the form and calls the survey.adp file as the POST action.

Here is the survey.html file, which consists of a simple form and a submit button which calls an ADP:

        <HTML>
        <HEAD>
          <TITLE>Survey Form</TITLE>
        </HEAD>
        <BODY BGCOLOR="#ffffff">
        <H2>
          Online Newsletter Subscription
        </H2>
        <P>
        <I>Sign up to be notified when this web site changes, or to
    receive an ASCII
        version via email. Thanks!</I>
        <FORM ACTION="survey.adp" METHOD="POST">
          <B>Name</B>
          <INPUT TYPE="text" NAME="name" SIZE="40">
          <P>
          <B>Title </B>
          <INPUT TYPE="text" NAME="title" SIZE="40" MAXLENGTH="80">
          <P>
          <INPUT TYPE="checkbox" NAME="notify" VALUE="1">Notify me by
    email when this newsletter changes online
          <P>
          <INPUT TYPE="checkbox" NAME="sendemail" VALUE="1">Send me an
    ASCII version of this newsletter by email
          <P>
          <B>Email Address
          </B>
          <INPUT TYPE="text" NAME="emailaddr" SIZE="40"
        MAXLENGTH="60">
          <P>
          <INPUT TYPE=submit>
        </FORM>
        <P>
        </BODY></HTML>

Here is the survey.adp file, which gets the form data from the survey, inserts it into the database, sends email to the subscription administrator and the user, and displays a confirmation message:

        <HTML>
        <HEAD>
          <TITLE>Subscription Processed Successfully</TITLE>
        </HEAD>
        <BODY BGCOLOR="#ffffff">
        <H2>
          Online Newsletter Subscription
        </H2>
        <P>
        Thank You for subscribing to our newsletter!
        <%
        # Get form data and assign to variables
        set r [ns_conn form $conn]
        set name [ns_set get $r name]
        set title [ns_set get $r title]
        set notify [ns_set get $r notify]
        set sendemail [ns_set get $r sendemail]
        set emailaddr [ns_set get $r emailaddr]

        # Set subscription options explicity to 0 if not checked
        if {$notify != 1} {set notify 0}
        if {$sendemail != 1} {set sendemail 0}

        # Update database with new subscription
        set db [ns_db gethandle]
        ns_db dml $db \
        "insert into test values ([ns_dbquotevalue $name],
    [ns_dbquotevalue $title], $notify, $sendemail,
    [ns_dbquotevalue $emailaddr])"

        # Send email message to subscription administrator
        set body "A new newsletter subscription was added for "
        append body $name
        append body ". The database has been updated."
        ns_sendmail "subscript@thecompany.com" "dbadmin@thecompany.com"
    \
        "New Subscription" $body

        # Send email message to user
        set body "Your online newsletter subscription has been successfully
    processed."
        ns_sendmail $emailaddr "dbadmin@thecompany.com" \
        "Your Online Subscription" $body

        # Show type of subscription to user
        if {$notify == 1} {
          ns_puts "You will be notified via email when the online newsletter
    changes."
        }
        if {$sendemail == 1} {
          ns_puts "Future issues of the newsletter will be sent to you
    via email."
        }
        %>
        </BODY></HTML>

#### Example 5: ADP sampler with includes, recursion, and streaming

The following HTML is an example of a page containing several Tcl scripts using the various ADP syntaxes. It invokes some Tcl functions, includes a file, executes another ADP, and uses streaming.

        <HTML>
        <HEAD>
        <TITLE>This is a test of ADP</TITLE>
        </HEAD>
        <BODY>
        <H1>This is a test of ADP</H1>
        <%

        ## Proxies should cache this page for a maximum of 1 hour:
        ns_setexpires $conn 3600
        set host [ns_set iget [ns_conn headers $conn] host]

        ## How many times has this page been accessed
        ## since the server was started?
        ns_share -init {set count 0} count
        incr count
        %>

        Number of accesses since server start: <%=$count%><br>
        tcl_version: <%=$tcl_version%><br>
        tcl_library: <%=$tcl_library%><br>
        Host: <%= $host %><br>

        <!-- Include the contents of a file: -->
        <%
        ns_adp_include standard-header
        %>

        <script language=tcl runat=server>
        ## You can do recursive ADP processing as well:
        ns_adp_include time.adp
        </script>

        <P>Here's an example of streaming:
        <script stream=on runat="server">
        ns_puts "<br>1...<br>"
        ns_sleep 2
        ns_puts "2...<br>"
        ns_sleep 2
        ns_puts "3!<br>"
        </script>
        <p><b>End</b>
        </BODY>
        </HTML>

The standard-header file referenced in the above example looks like this:

        This is a standard header.

The time.adp file referenced in the example looks like this:

        <p>The time is: <%=[ns_httptime [ns_time]]%><p>

Because of the streaming used in the last script, the "1...", "2...", "3!" and "End" part of the page will be displayed gradually.

Top of Page
Copyright © 1998-99 America Online, Inc.