Author Topic: ScriptBasic CGI Programming Tutorial  (Read 47041 times)

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
ScriptBasic CGI Programming Tutorial
« on: January 04, 2008, 01:10:49 AM »
Part 1 - Hello World

This thread will be an on-going tutorial on how to use ScriptBasic for your CGI scripting needs. I will start this first post with the standard "Hello World" with the minimal amount of code needed for ScriptBasic (scriba) to generate a dynamic page. It is assumed that you have ScriptBasic installed and configured correctly. (another tutorial at a later date)

Code: [Select]
#! /usr/bin/scriba -c

INCLUDE cgi.bas

OPTION cgi$Method cgi::Get

cgi::Header(200, "text/html")
cgi::FinishHeader()

a = "Hello World !"

PRINT """
<html>
<header>
<title>ScriptBasic CGI</title>
</header>
<body>
<center>
"""

PRINT "<h1>" & a & "</h1>\n"

PRINT """
</center>
</body>
</html>
"""

END

#! /usr/bin/scriba -c
The first line of your program starts the ScriptBasic interpreter and the -c option tells scriba that this is a CGI program.

INCLUDE cgi.bas
The cgi.bas include file contains both ScriptBasic code and declares to C functions in the cgi.dll (.so) shared library. Access to cgi.bas functions and variables within it's name space is accomplished by using the cgi:: reference.
(similar to Perl)

OPTION cgi$Method cgi::Get
The OPTION statement is used to define what CGI methods are valid for the page. In our example, only GET request will be allowed. The following constants are available as an a argument for the OPTION statement. You may combine values using the OR operator.
Code: [Select]
' constants for setting option cgi$Method
const None   = &H00000000
const Get    = &H00000001
const Post   = &H00000002
const Upload = &H00000006
const Put    = &H00000008
const Del    = &H00000010
const Copy   = &H00000020
const Move   = &H00000040
const Head   = &H00000080

Header() & FinishHeader() functions
These two functions build the the basic page header that all HTML pages require.

a = "Hello World !"
Here we assign a variable with a string value. Variables do not have to have their type declared or dimensioned using a DIM statement.

PRINT """   """
The PRINT statement outputs to the browser whatever is between the triple quotes verbatim as it is in your program keeping what formating you have used.

PRINT "<h1>" & a & "</h1>\n"
The & character is used to concatenate the quoted text and the string variable holding our "Hello World !" stored value. The \n causes ScriptBasic to print a new line character. (more on using the \ character later)

END
The terminates the ScriptBasic program, ending it's connection with the web server. (optional end of program keyword)
« Last Edit: June 26, 2014, 06:24:57 PM by support »

verhas

  • Guest
tripple quotes
« Reply #1 on: January 06, 2008, 01:53:37 AM »
Triple quotes came from the language Python. String started/ended using them can also be multi-line, and that is the preferred way to do.

verhas

  • Guest
Forum vs Wiki
« Reply #2 on: January 06, 2008, 01:54:57 AM »
I welcome anyone starting a tutorial like that. I believe that Wiki is the right place for that.

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
ScriptBasic CGI Programming Tutorial
« Reply #3 on: January 06, 2008, 02:09:41 AM »
Hi Peter,

Quote from: "Peter Verhas"

I welcome anyone starting a tutorial like that. I believe that Wiki is the right place for that.


Using the forum to generate the initial tutorial allows more interaction with the other forum members. Once completed, as you say, the wiki is where this should end up.

Thanks for the feedback !

John

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
ScriptBasic CGI Programming Tutorial
« Reply #4 on: January 07, 2008, 02:24:38 AM »
Part 2 - Working with MySQL

In part 2, I'm going to show how to access MySQL and display the result set in a table. The MySQL extension is a C library interface to the database engine. This example will show you how easy it is to work with a SQL database interface.

Test Table Definition



Code: [Select]

#!/usr/bin/scriba -c

INCLUDE cgi.bas
INCLUDE mysql.bas

OPTION cgi$Method cgi::Get

dbh = mysql::RealConnect("host","user","password","database")

cgi::Header 200,"text/html"
cgi::FinishHeader

PRINT """
<HTML>
<HEAD>
<title>MySQL Tutorial</title>
</HEAD>
<BODY>
<FONT face="Verdana, Arial, Helvetica, sans-serif">
<TABLE border="1" cellpadding="3">
"""

mysql::query(dbh,"SELECT * FROM contact")
WHILE mysql::FetchHash(dbh, col_name)

PRINT "<TR>\n"
PRINT "<TD>",col_name{"ID"},"</TD>\n"
PRINT "<TD>",col_name{"NAME"},"</TD>\n"
PRINT "<TD>",col_name{"ADDRESS"},"</TD>\n"
PRINT "<TD>",col_name{"CITY"},"</TD>\n"
PRINT "<TD>",col_name{"STATE"},"</TD>\n"
PRINT "<TD>",col_name{"ZIP"},"</TD>\n"
PRINT "<TD>",col_name{"PHONE"},"</TD>\n"
PRINT "<TD>",col_name{"EMAIL"},"</TD>\n"
PRINT "<TD>",col_name{"URL"},"</TD>\n"
PRINT "</TR>\n"

WEND

PRINT """
</TABLE>
</FONT>
</BODY>
</HTML>
"""

mysql::Close(dbh)

END


Run Program

INCLUDE mysql.bas
The mysql.bas include file declares the functions to access the MySQL library.

dbh = mysql::RealConnect("host","user","password","database")
The MySQL library call makes the connection to the database. If the database is defined in the basic.conf file then you can use an abbreviated version of the function to connect to the MySQL interface.
Code: [Select]
mysql::Connect("conf_defined_name")
This moves the login information out of the program and into a binary configuration file. The dbh variable contains the connection handle and shouldn't be modified by the programmer. It is used to pass to the other mysql:: functions.

mysql::query(dbh,"SELECT * FROM contact")
This function passes your SQL statement to MySQL. If a query is passed, then you could use the AffectedRows() function to return how many rows in the record set.
Code: [Select]
number_of_rows = mysql::AffectedRows(dbh)

WHILE mysql::FetchHash(dbh, col_name)
The WHILE directive will loop through the record set until the FetchHash() function returns false. This version of fetch uses an associative array to store the row from the record set. This allows you to reference the data by the column name. (case sensitive) There are times when this may not be practicle. (like a JOIN) In this case you would use the FetchArray() function to return the row from the result set.
Code: [Select]
WHILE mysql::FetchArray(dbh, array_var)

PRINT "<TR>\n"
PRINT "<TD>",array_var[0],"</TD>\n"
PRINT "<TD>",array_var[1],"</TD>\n"
PRINT "<TD>",array_var[2],"</TD>\n"
PRINT "<TD>",array_var[3],"</TD>\n"
PRINT "<TD>",array_var[4],"</TD>\n"
PRINT "<TD>",array_var[5],"</TD>\n"
PRINT "<TD>",array_var[6],"</TD>\n"
PRINT "<TD>",array_var[7],"</TD>\n"
PRINT "<TD>",array_var[8],"</TD>\n"
PRINT "</TR>\n"

WEND



PRINT "<TD>",col_name{"ID"},"</TD>\n"
This will populate the cell with the current row "ID" column data.

WEND
This defines the end of the WHILE loop.

mysql::Close(dbh)
This explicitly closes the MySQL connection. ScriptBasic will close all database connections on it's own upon exiting the script.

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
ScriptBasic CGI Programming Tutorial
« Reply #5 on: January 13, 2008, 01:16:30 AM »
Part 3 - Working With Forms

In Part 3 we will be creating a simple form that will ask for the user name. If submit is clicked without entering anything a error message will display allowing re-entry. If they type something in then the page will display "Hello user_name". If they click on the cancel button, they are taken to the ScriptBasic home page. All this in one small page of code.

sbform.bas
Code: [Select]

#! /usr/bin/scriba -c

INCLUDE cgi.bas

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

err_msg = 0

IF cgi::RequestMethod() = "POST" AND cgi::PostParam("nav") = "Cancel" THEN
  cgi::Header(302, "text/html")
  PRINT "Location: /home/index.php\n"
  cgi::FinishHeader()
  END
END IF

cgi::Header(200, "text/html")
cgi::FinishHeader()

PRINT """
<html>
<header>
<title>ScriptBasic Forms</title>
<script type="text/javascript">
   function focusit() {
    var tempval=eval(document.getElementById('name'))
    tempval.focus()
    tempval.select()
  }
</script>
</header>
"""

IF cgi::RequestMethod() = "POST" THEN
  IF cgi::PostParam("my_name") > "" THEN
    PRINT "<body>\n"
    PRINT "<h2>Hello " & cgi::PostParam("my_name") & ".</h2><br>\n"
    GOTO All_Done
  ELSE
    err_msg = 1
  END IF
END IF

PRINT """
<body onLoad="javascript:focusit();">
<form action="/cgi-bin/sbform.bas" method="POST">
<h2>What is your name?</h2>
<input type="text" name="my_name" size="30" id="name"><br>
<br>
<input type="submit" name="nav" value="Submit">&nbsp;<input type="submit" name="nav" value="Cancel">
</form>
"""

IF err_msg THEN PRINT """<br><h2><font color="red">Please enter your name!</font></h2>\n"""

All_Done:

PRINT """
</body>
</html>
"""

END


Run Program

cgi::RequestMethod()
This function determines how the page was called. (Get or Post)


cgi::PostParam("my_name")
This function returns the value for the form field with a name="my_name".


PRINT "<h2>Hello " & cgi::PostParam("my_name") & ".</h2><br>\n"
This works as well.
Code: [Select]

PRINT "<h2>Hello ",cgi::PostParam("my_name"),".</h2><br>\n"


Page Redirection
Code: [Select]

cgi::Header(302, "text/html")
PRINT "Location: /home/index.php\n"
cgi::FinishHeader()


This header will redirect the page to the URL specified by "Location:".

DJBenson31

  • Guest
Re: ScriptBasic CGI Programming Tutorial
« Reply #6 on: March 22, 2010, 09:44:39 AM »
Thank you for this tutorial!! It really helped demystify some of ScriptBasics features.

Support

  • Administrator
  • *****
  • Posts: 19
    • View Profile
Re: ScriptBasic CGI Programming Tutorial
« Reply #7 on: March 22, 2010, 01:11:51 PM »
Glad to hear that this tutorial helped in some way.

Are you running the ScriptBasic webserver or using scriba as a CGI scripting language? What OS you running under?