ScriptBasic > Tutorials

CGI Programming II

<< < (2/2)

Support:
I haven't been much of a AJAX fan due to the layers of protocol getting something simple like a remote/hidden submit to work in my applications. I stumbled across a really nice AJAX framework that I thought I would share and include as part of this tutorial.

Prototype JavaScript Framework



--- Quote from: Bruce Perry - www.xml.com ---Why Prototype?

Why didn't I just create a plain old JavaScript object (POJO) for my application, instead of introducing an open source library? For one, Prototype includes a nifty collection of JavaScript shortcuts that reduce typing and help avoid the reinvention of the wheel. The commonly touted shortcut is $("mydiv"), which is a Prototype function that returns a Document Object Model (DOM) Element associated with the HTML tag with id "mydiv". That sort of concision alone is probably worth the cost of setting up Prototype. It's the equivalent of:

document.getElementById("mydiv");

Another useful Prototype shortcut is $F("mySelect"), for returning the value of an HTML form element on a web page, such as a selection list. Once you get used to Prototype's austere, Perlish syntax, you will use these shortcuts all the time. Prototype also contains numerous custom objects, methods, and extensions to built-in JavaScript objects, such as the Enumeration and Hash objects (which I discuss below).

Finally, Prototype also wraps the functionality of XMLHttpRequest with its own Ajax.Request and related objects, so that you don't have to bother with writing code for instantiating this object for various browsers.

--- End quote ---


--- Quote ---Try.these

Try.these(Function...) -> firstOKResult

Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.

--- End quote ---


More on AJAX soon ...

Your probably saying at this point, "What does JavaScript & AJAX have to do with CGI programming with Basic." If your interested in creating desktop like applications in a browser then what CGI is to the server, JavaScript (AJAX) is to the client. Your CGI applications are responsible for dynamically creating HTML/JavaScript and sending it back to the client. (a stream of text which your browser interprets to render your pages) You can request a static HTML page, a page dynamically created based on criteria and a page with embedded localized/remote scripting.

Support:
I recently had to write a shopping cart for a client's site running ScriptBasic and thought I would add it to my CGI tutorial.

This cart can add / remove items and calculates the total with a check for a minimum quantity of 5.


--- Code: ---' Program: cart
' Version: 1.0
'    Date: 2009-08-15
'      By: JRS

GLOBAL CONST NL = "\n"

INCLUDE cgi.bas
INCLUDE mysql.bas
INCLUDE mt.bas
INCLUDE t.bas

OPTION cgi$Method cgi::Get OR cgi::Post

session_id = cgi::Cookie("CART")

New_Session:

IF session_id = undef THEN
  session_id = mt::NewSessionId()
  mt::SetSessionId(session_id)
ELSE
  IF mt::CheckSessionId(session_id) = False THEN
    session_id = undef
    GOTO New_Session
  END IF
  mt::SetSessionId(session_id)
END IF

dbh = mysql::Connect("CART_DB")

' *** START GET ***
IF cgi::RequestMethod() = "GET" THEN

' Check for Add/Remove/Display Mode
module_number = cgi::GetParam("module")
IF module_number = undef THEN
  cartmode = "display"
ELSE
  cartmode = "add"
END IF

' Check for existing cart array
cartstr = mt::GetSessionVariable("cartstr")
IF cartstr = undef THEN
  itemcount = 0
ELSE
  t::StringToArray(mycart, cartstr)
  itemcount = UBOUND(mycart)
END IF
IF cartmode = "add" THEN itemcount += 1

' REMOVE Mode
delmod = cgi::GetParam("remove")
IF delmod >= 1 AND delmod <= itemcount THEN
  thisline = 1
  UNDEF tmpcart
  FOR x = 1 TO itemcount
    IF delmod <> x THEN
      tmpcart[thisline,0] = thisline
      tmpcart[thisline,1] = mycart[x,1]
      tmpcart[thisline,2] = mycart[x,2]
      tmpcart[thisline,3] = mycart[x,3]
      tmpcart[thisline,4] = mycart[x,4]
      tmpcart[thisline,5] = mycart[x,5]
      thisline += 1
    END IF
  NEXT x
  UNDEF mycart 
  mycart = tmpcart
  itemcount -= 1
  IF itemcount = 0 THEN
    UNDEF cartstr, mycart
    mt::SetSessionVariable("cartstr", undef)
  ELSE
    cartstr = t::ArrayToString(mycart)
    mt::SetSessionVariable("cartstr", cartstr)
  END IF
END IF

' ADD Mode
IF cartmode = "add" THEN
  mycart[itemcount, 0] = itemcount
  mycart[itemcount, 1] = module_number
  mysql::query(dbh,"SELECT PTITLE,PPRICE FROM Modules WHERE PNUM = '" & module_number & "'")
  mysql::FetchArray(dbh, modrow)
  mycart[itemcount, 2] = modrow[0]
  mycart[itemcount, 3] = modrow[1]
  mycart[itemcount, 4] = 5
  mycart[itemcount, 5] = FORMAT("%~####.00~", 5 * modrow[1])
  cartstr = t::ArrayToString(mycart)
  mt::SetSessionVariable("cartstr", cartstr)
END IF

' Cart Empty
IF ISUNDEF(mycart) THEN
  cgi::Header 302,"text/html"
  PRINT "Location: /home/curricula\n"
  cgi::SetCookie("CART", session_id, undef, undef, gmtime() + 2700, false)
  cgi::FinishHeader
  END
END IF
' *** END GET ***

ELSE

' *** START POST ***
IF cgi::PostParam("button") = "Continue to Checkout" THEN
  cgi::Header 302,"text/html"
  PRINT "Location: /home/checkout\n"
  cgi::SetCookie("CART", session_id, undef, undef, gmtime() + 2700, false)
  cgi::FinishHeader
  END
END IF

cartstr = mt::GetSessionVariable("cartstr")
t::StringToArray(mycart, cartstr)
itemcount = UBOUND(mycart)
FOR x = 1 TO itemcount
  mycart[x, 4] = cgi::PostParam("line" & x)
  IF VAL(mycart[x,4]) < 5 THEN
    mycart[x,4] = 5
  END IF
  mycart[x, 5] = FORMAT("%~####.00~", mycart[x,4] * mycart[x,3])
NEXT x
cartstr = t::ArrayToString(mycart)
mt::SetSessionVariable("cartstr", cartstr)
END IF
' *** END POST ***

' Build Standard Header
cgi::Header(200, "text/html")
cgi::SetCookie("CART", session_id, undef, undef, gmtime() + 2700, false)
cgi::FinishHeader()

PRINT """
<HTML>
<HEAD>
<TITLE>Order Cart</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<STYLE type=text/css>A {
TEXT-DECORATION: underline
}
A:active { + 1
COLOR: #103366; TEXT-DECORATION: underline
} - 1
A:hover {
COLOR: #103366; TEXT-DECORATION: underline
}
A:visited {
COLOR: #666666; TEXT-DECORATION: underline
}
</STYLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF text="#000000" link="#103366" vlink="#666666" leftmargin="0" topmargin="0" alink="#666666"  background="/media/CART/images/background.gif">
"""

INCLUDE "header"

PRINT """ 
<TABLE WIDTH=775 BORDER=0 CELLPADDING=0 CELLSPACING=0>
  <TR>
    <TD VALIGN=TOP bgcolor="#103366" width="70" height="523" style="border:1px solid; border-color : #000000;">
"""

INCLUDE "left_nav"

PRINT """
    </TD>
    <TD VALIGN=TOP WIDTH=757 height="523" bgcolor="#FFFFFF">
      <table border=0 cellspacing=0 cellpadding=0 width=672>
        <tr align="right">
          <td height="22" colspan=3>&nbsp;</td>
        </tr>
        <tr valign="top">
          <td valign="top">
            <TABLE CELLSPACING="0" CELLPADDING="18" BORDER="0"><TR VALIGN="TOP"><TD WIDTH="100%" ALIGN="LEFT">
            <font color="#000000" size="2" face="verdana"><a href="/home/curricula">Return to Section Listing</a></font><BR><BR>
            <font color="#000000" size="2" face="verdana">
            <BR>
            <FONT face="verdana,arial" size=3 style="font-size:18px;" class=font.normal color="#103366"><B>Order Cart</B></font><BR><BR>
            <TABLE border=1 cellspacing=2 cellpadding=4 width=100%>
            <form method=post action="/home/cart">
              <TR VALIGN="Top">
                <TD WIDTH=80 align=center bgcolor="#103366"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal color=#FFFFFF><B>Module #</B></font><BR><IMG SRC="/media/CART/images/spacer.gif" WIDTH=80 HEIGHT=1 border=0></td>
                <TD WIDTH=100% align=left bgcolor="#103366"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal color=#FFFFFF><B>Module Title</B></font></td>
                <TD WIDTH=80 align=center bgcolor="#103366"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal color=#FFFFFF><B>Price</B></font><BR><IMG SRC="/media/CART/images/spacer.gif" WIDTH=80 HEIGHT=1 border=0></td>
                <TD WIDTH=60 align=center bgcolor="#103366"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal color=#FFFFFF><B>Quantity</B></font><BR><IMG SRC="/media/CART/images/spacer.gif" WIDTH=60 HEIGHT=1 border=0></td>
                <TD WIDTH=80 align=center bgcolor="#103366"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal color=#FFFFFF><B>Subtotal</B></font><BR><IMG SRC="/media/CART/images/spacer.gif" WIDTH=80 HEIGHT=1 border=0></td>
              </tr>
""" 
FOR x = 1 TO itemcount
PRINT """
              <TR VALIGN="Top">
                <TD WIDTH=80 align=center><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal>""" & mycart[x,1] & """<BR><a href="/home/cart?remove=""" & mycart[x,0] & """\">REMOVE</a></font></td>
                 <TD WIDTH=200 align=left><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>""" & mycart[x,2] & """</B></font></td>
                <TD WIDTH=80 align=center><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal>""" & FORMAT("%~$##.00~",mycart[x,3]) & """</font></td>
                <TD WIDTH=60 align=center><input type=text name="line""" & x & """\" value=\"""" & mycart[x,4] & """\" size="4" style="text-align:right;"></td>
                <TD WIDTH=80 align=right><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>""" & FORMAT("%~$#,###.00~",mycart[x,5]) & """</B></font></td>
              </tr>
              <TR VALIGN="Top">
                <TD colspan=4 align=right bgcolor="#103366"><IMG SRC="/media/CART/images/spacer.gif" WIDTH=10 HEIGHT=1 border=0></td>
              </tr>
"""
NEXT x

total_qty = 0
total_amt = 0

FOR x = 1 TO itemcount
  total_qty = total_qty + mycart[x,4]
  total_amt = total_amt + mycart[x,5]
NEXT x   

IF total_qty >= 5  AND total_qty <= 10 THEN
  shipping = 5.00
ELSE IF total_qty >= 11 AND total_qty <= 25 THEN
  shipping = 10.00
ELSE IF total_qty >= 26 AND total_qty <= 50 THEN
  shipping="15.00"
ELSE
  shipping="20.00"
END IF

PRINT """
              <TR VALIGN="Top">
                <TD colspan=4 align=right bgcolor="#e1e1e1"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>Subtotal</B></font><IMG SRC="/media/CART/images/spacer.gif" WIDTH=10 HEIGHT=1 border=0></td>
                <TD WIDTH=80 align=right><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>""" & FORMAT("%~$#,###.00~",total_amt) & """</B></font><BR><IMG SRC="/media/CART/images/spacer.gif" WIDTH=80 HEIGHT=1 border=0></td>
              </tr>

              <TR VALIGN="Top">
                <TD colspan=4 align=right bgcolor="#e1e1e1"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>Shipping & Handling<BR></B></font><IMG SRC="/media/CART/images/spacer.gif" WIDTH=10 HEIGHT=1 border=0></td>
                <TD WIDTH=80 align=right><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>""" & FORMAT("%~$#,###.00~",shipping) & """</B></font><BR><IMG SRC="/media/CART/images/spacer.gif" WIDTH=80 HEIGHT=1 border=0></td>
              </tr>
"""

total_inv = total_amt + shipping

PRINT """
              <TR VALIGN="Top">
                <TD colspan=4 align=right bgcolor="#e1e1e1"><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>Total Due</B></font><IMG SRC="/media/CART/images/spacer.gif" WIDTH=10 HEIGHT=1 border=0></td>
                <TD WIDTH=80 align=right><FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><B>""" & FORMAT("%~$#,###.00~",total_inv) & """</B></font><BR><IMG SRC="/media/CART/images/spacer.gif" WIDTH=80 HEIGHT=1 border=0></td>
              </tr>
            </table>
            <BR><BR>
            <FONT face="verdana,arial" size=1 style="font-size:10px;" class=font.normal><input type=submit name=button value="Change Quantities"> You can modify all of the quantity boxes, then click here. <BR><BR>
            <input type=submit name=button value="Continue to Checkout"></form>
            <BR><BR><BR><BR></font><br>
          </TD>
        </TR>
      </TABLE>
      <p><font size="2" face="verdana"> </font><br>
      <font size="2" face="verdana">&nbsp;</font>
      <center>
      </center></p>
    </td>
  </tr>
  <tr>
  </tr>
 </table>
    </TD></TR><tr><TD COLSPAN=5 bgcolor="#0C0533" height="20" align="center"><font color="#CCCCCC" face="Verdana, Arial, Helvetica, sans-serif" size="1">Copyright © 2000-2009&nbsp;&nbsp;DBA Company</font></td>
</tr></TABLE>
</center>
<map name="Map3">
  <area shape="rect" coords="5,0,283,91" href="/home/start">
</map>
</BODY>
</HTML>
"""

--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version