ScriptBasic > Tutorials

ScriptBasic CGI Programming Tutorial

<< < (2/2)

Support:
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: ---
#! /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

--- End code ---


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: ---
PRINT "<h2>Hello ",cgi::PostParam("my_name"),".</h2><br>\n"
--- End code ---


Page Redirection

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

--- End code ---


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

DJBenson31:
Thank you for this tutorial!! It really helped demystify some of ScriptBasics features.

Support:
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?

Navigation

[0] Message Index

[*] Previous page

Go to full version