+ All Categories
Home > Documents > Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents...

Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents...

Date post: 12-Apr-2018
Category:
Upload: lamngoc
View: 227 times
Download: 3 times
Share this document with a friend
30
Basic HTML
Transcript
Page 1: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Basic HTML

What and Where

bull Our biolinx computer has a web server on it ldquoApacherdquo is the brand name it is Open Source software and it is probably the most common web server in existence

bull From a practical point of view the web server makes all files located in thesrvwwwhtdocsbiolinxhtml directory (and any sub-directories under it) visible to the World Wide Web Pointing your web browser at httpbiolinxbiosniuedu gives you access to this directory

bull For example look at the ldquohellohtmlrdquo file from within biolinx (srvwwwhtdocsbiolinxhtmlhellohtml) and from your web browser ( httpbiolinxbiosniueduhellohtml ) They are the same file Try comparing the source code using ldquoView Sourcerdquo in your web browser However we can manipulate the file from inside biolinx from the Web all we can do is look at it

bull You each have your own sub directory for HTML srvwwwhtdocsbiolinxhtmlz012345 (or whatever your z-number is) viewed through the web as httpbiolinxbiosniueduz012345 Put all your HTML documents in this directory

What is HTML

bull Hyper Text Markup Language is a ldquomarkup languagerdquo It is a set of instructions to your web browser to cause the text to be displayed in a certain way

bull HTML is not a programming language in that it doesnrsquot allow decisions (if statements) or loops

bull You can see what the actual HTML document looks like (as opposed to how it is displayed) using the ldquoView Sourcerdquo control on the browser

bull HTML is a subset of SGML Standard Generalized Markup Language which is a generic way of representing any document SGML is more or less too complicated to be useful but it has spawned two important subsets HTML and XML (which we will discuss later

HTML Standards

bull HTML is an evolving language I am presenting approximately HTML version 32 which is quite simple but which should work with all current browsers We want to be able to generate HTML documents ldquoon the flyrdquo from programs written in Perl to display data dynamically This is best done using simple HTML rather than the more complex forms used by large commercial web pages

bull HTML 40 a more recent version has ldquodeprecatedrdquo many of the tags that determine style (notably the ltfontgt tag) and asks that you put style information in ldquoCascading Style Sheetsrdquo Despite the deprecation billions of web documents were (and continue to be) written without style sheets For this reason all browsers continue to support older version of HTML and will do so for the indefinite future However HTML 401 which was released in late 1997 is the current standard for the web

bull ldquoDeprecatedrdquo means that there is a newer and better way of marking up the information than the old tag However deprecated tags still work ldquoObsoleterdquo tags may not work

bull XHTML (Extensible HTML) is still being developed It is an attempt to convert HTML into XML Version 10 has been released

Document Type Definition

bull HTML standards are defined in documents called DTDs (document type definitions) There is a default DTD used by the browser and thus we donrsquot have to explicitly define a DTD All XML documents come with a separate DTD file

bull If desired we can explicitly used a DTD by starting the HTML file with the line

ltDOCTYPE HTML PUBLIC -W3CDTD HTML 401 TransitionalENgt bull This line says that the document follows the guidelines of the World Wide

Web Consortium (W3C) transitional standards for HTML 401 ldquoTransitionalrdquo means that some HTML 32 is still involved W3C is the body that sets standards for the web

bull However you should be aware that approximately 90 of the browsers these days are Microsoftrsquos Internet Explorer This semi-monopoly allows Microsoft to ignore standards or create its own at will

bull In practical terms a web site that displays correctly for both Internet Explorer and Mozilla Firefox will probably cover just about all situations IE because of the above-stated Microsoft 900-pouind-gorilla problem and Firefox because it follows the W3C standards that all other browser use

HTML Tags

bull The basic feature in HTML documents is the ldquotagrdquo bull Tags are set off by angle brackets (ldquoltldquo and ldquogtrdquo) with the tag

name between them For example the entire HTML document is placed between the opening tag lthtmlgt and the closing tag lthtmlgt

bull Most tags occur in pairs indicating what is supposed to happen to whatever text is between them The closing tag has the same name as the opening tag but the closing tag stars with a slash () For example ltbgtmake this boldltbgt The text between the ltbgt and ltbgt tags is made boldface by the browser

bull Pairs of tags are supposed to be nested you close all inner tags before closing outer tags Thus

ltbgtltigtbold and italicizeltigtltbgt CORRECT ltbgtltigtbold and italicizeltbgtltigt WRONG

More on Tags

bull Opening tags often contain attributes as well as tag names Attributes are separated from each other by spaces and they are in the form of name=ldquovaluerdquo For example lth2 align=ldquocenterrdquogtTitlelth2gt creates a centered headline The default is left-justified

bull HTML tags are case-insensitive lttablegt ltTABLEgt and lttAbLEgt are all equivalent However the current XHTML standard suggests that we should use small letters lttablegt

bull Some tags donrsquot have a closing tag ltbrgt a line break is a common example The XHTML standard suggests putting a slash into the single tag in these cases ltbr gt

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 2: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

What and Where

bull Our biolinx computer has a web server on it ldquoApacherdquo is the brand name it is Open Source software and it is probably the most common web server in existence

bull From a practical point of view the web server makes all files located in thesrvwwwhtdocsbiolinxhtml directory (and any sub-directories under it) visible to the World Wide Web Pointing your web browser at httpbiolinxbiosniuedu gives you access to this directory

bull For example look at the ldquohellohtmlrdquo file from within biolinx (srvwwwhtdocsbiolinxhtmlhellohtml) and from your web browser ( httpbiolinxbiosniueduhellohtml ) They are the same file Try comparing the source code using ldquoView Sourcerdquo in your web browser However we can manipulate the file from inside biolinx from the Web all we can do is look at it

bull You each have your own sub directory for HTML srvwwwhtdocsbiolinxhtmlz012345 (or whatever your z-number is) viewed through the web as httpbiolinxbiosniueduz012345 Put all your HTML documents in this directory

What is HTML

bull Hyper Text Markup Language is a ldquomarkup languagerdquo It is a set of instructions to your web browser to cause the text to be displayed in a certain way

bull HTML is not a programming language in that it doesnrsquot allow decisions (if statements) or loops

bull You can see what the actual HTML document looks like (as opposed to how it is displayed) using the ldquoView Sourcerdquo control on the browser

bull HTML is a subset of SGML Standard Generalized Markup Language which is a generic way of representing any document SGML is more or less too complicated to be useful but it has spawned two important subsets HTML and XML (which we will discuss later

HTML Standards

bull HTML is an evolving language I am presenting approximately HTML version 32 which is quite simple but which should work with all current browsers We want to be able to generate HTML documents ldquoon the flyrdquo from programs written in Perl to display data dynamically This is best done using simple HTML rather than the more complex forms used by large commercial web pages

bull HTML 40 a more recent version has ldquodeprecatedrdquo many of the tags that determine style (notably the ltfontgt tag) and asks that you put style information in ldquoCascading Style Sheetsrdquo Despite the deprecation billions of web documents were (and continue to be) written without style sheets For this reason all browsers continue to support older version of HTML and will do so for the indefinite future However HTML 401 which was released in late 1997 is the current standard for the web

bull ldquoDeprecatedrdquo means that there is a newer and better way of marking up the information than the old tag However deprecated tags still work ldquoObsoleterdquo tags may not work

bull XHTML (Extensible HTML) is still being developed It is an attempt to convert HTML into XML Version 10 has been released

Document Type Definition

bull HTML standards are defined in documents called DTDs (document type definitions) There is a default DTD used by the browser and thus we donrsquot have to explicitly define a DTD All XML documents come with a separate DTD file

bull If desired we can explicitly used a DTD by starting the HTML file with the line

ltDOCTYPE HTML PUBLIC -W3CDTD HTML 401 TransitionalENgt bull This line says that the document follows the guidelines of the World Wide

Web Consortium (W3C) transitional standards for HTML 401 ldquoTransitionalrdquo means that some HTML 32 is still involved W3C is the body that sets standards for the web

bull However you should be aware that approximately 90 of the browsers these days are Microsoftrsquos Internet Explorer This semi-monopoly allows Microsoft to ignore standards or create its own at will

bull In practical terms a web site that displays correctly for both Internet Explorer and Mozilla Firefox will probably cover just about all situations IE because of the above-stated Microsoft 900-pouind-gorilla problem and Firefox because it follows the W3C standards that all other browser use

HTML Tags

bull The basic feature in HTML documents is the ldquotagrdquo bull Tags are set off by angle brackets (ldquoltldquo and ldquogtrdquo) with the tag

name between them For example the entire HTML document is placed between the opening tag lthtmlgt and the closing tag lthtmlgt

bull Most tags occur in pairs indicating what is supposed to happen to whatever text is between them The closing tag has the same name as the opening tag but the closing tag stars with a slash () For example ltbgtmake this boldltbgt The text between the ltbgt and ltbgt tags is made boldface by the browser

bull Pairs of tags are supposed to be nested you close all inner tags before closing outer tags Thus

ltbgtltigtbold and italicizeltigtltbgt CORRECT ltbgtltigtbold and italicizeltbgtltigt WRONG

More on Tags

bull Opening tags often contain attributes as well as tag names Attributes are separated from each other by spaces and they are in the form of name=ldquovaluerdquo For example lth2 align=ldquocenterrdquogtTitlelth2gt creates a centered headline The default is left-justified

bull HTML tags are case-insensitive lttablegt ltTABLEgt and lttAbLEgt are all equivalent However the current XHTML standard suggests that we should use small letters lttablegt

bull Some tags donrsquot have a closing tag ltbrgt a line break is a common example The XHTML standard suggests putting a slash into the single tag in these cases ltbr gt

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 3: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

What is HTML

bull Hyper Text Markup Language is a ldquomarkup languagerdquo It is a set of instructions to your web browser to cause the text to be displayed in a certain way

bull HTML is not a programming language in that it doesnrsquot allow decisions (if statements) or loops

bull You can see what the actual HTML document looks like (as opposed to how it is displayed) using the ldquoView Sourcerdquo control on the browser

bull HTML is a subset of SGML Standard Generalized Markup Language which is a generic way of representing any document SGML is more or less too complicated to be useful but it has spawned two important subsets HTML and XML (which we will discuss later

HTML Standards

bull HTML is an evolving language I am presenting approximately HTML version 32 which is quite simple but which should work with all current browsers We want to be able to generate HTML documents ldquoon the flyrdquo from programs written in Perl to display data dynamically This is best done using simple HTML rather than the more complex forms used by large commercial web pages

bull HTML 40 a more recent version has ldquodeprecatedrdquo many of the tags that determine style (notably the ltfontgt tag) and asks that you put style information in ldquoCascading Style Sheetsrdquo Despite the deprecation billions of web documents were (and continue to be) written without style sheets For this reason all browsers continue to support older version of HTML and will do so for the indefinite future However HTML 401 which was released in late 1997 is the current standard for the web

bull ldquoDeprecatedrdquo means that there is a newer and better way of marking up the information than the old tag However deprecated tags still work ldquoObsoleterdquo tags may not work

bull XHTML (Extensible HTML) is still being developed It is an attempt to convert HTML into XML Version 10 has been released

Document Type Definition

bull HTML standards are defined in documents called DTDs (document type definitions) There is a default DTD used by the browser and thus we donrsquot have to explicitly define a DTD All XML documents come with a separate DTD file

bull If desired we can explicitly used a DTD by starting the HTML file with the line

ltDOCTYPE HTML PUBLIC -W3CDTD HTML 401 TransitionalENgt bull This line says that the document follows the guidelines of the World Wide

Web Consortium (W3C) transitional standards for HTML 401 ldquoTransitionalrdquo means that some HTML 32 is still involved W3C is the body that sets standards for the web

bull However you should be aware that approximately 90 of the browsers these days are Microsoftrsquos Internet Explorer This semi-monopoly allows Microsoft to ignore standards or create its own at will

bull In practical terms a web site that displays correctly for both Internet Explorer and Mozilla Firefox will probably cover just about all situations IE because of the above-stated Microsoft 900-pouind-gorilla problem and Firefox because it follows the W3C standards that all other browser use

HTML Tags

bull The basic feature in HTML documents is the ldquotagrdquo bull Tags are set off by angle brackets (ldquoltldquo and ldquogtrdquo) with the tag

name between them For example the entire HTML document is placed between the opening tag lthtmlgt and the closing tag lthtmlgt

bull Most tags occur in pairs indicating what is supposed to happen to whatever text is between them The closing tag has the same name as the opening tag but the closing tag stars with a slash () For example ltbgtmake this boldltbgt The text between the ltbgt and ltbgt tags is made boldface by the browser

bull Pairs of tags are supposed to be nested you close all inner tags before closing outer tags Thus

ltbgtltigtbold and italicizeltigtltbgt CORRECT ltbgtltigtbold and italicizeltbgtltigt WRONG

More on Tags

bull Opening tags often contain attributes as well as tag names Attributes are separated from each other by spaces and they are in the form of name=ldquovaluerdquo For example lth2 align=ldquocenterrdquogtTitlelth2gt creates a centered headline The default is left-justified

bull HTML tags are case-insensitive lttablegt ltTABLEgt and lttAbLEgt are all equivalent However the current XHTML standard suggests that we should use small letters lttablegt

bull Some tags donrsquot have a closing tag ltbrgt a line break is a common example The XHTML standard suggests putting a slash into the single tag in these cases ltbr gt

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 4: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

HTML Standards

bull HTML is an evolving language I am presenting approximately HTML version 32 which is quite simple but which should work with all current browsers We want to be able to generate HTML documents ldquoon the flyrdquo from programs written in Perl to display data dynamically This is best done using simple HTML rather than the more complex forms used by large commercial web pages

bull HTML 40 a more recent version has ldquodeprecatedrdquo many of the tags that determine style (notably the ltfontgt tag) and asks that you put style information in ldquoCascading Style Sheetsrdquo Despite the deprecation billions of web documents were (and continue to be) written without style sheets For this reason all browsers continue to support older version of HTML and will do so for the indefinite future However HTML 401 which was released in late 1997 is the current standard for the web

bull ldquoDeprecatedrdquo means that there is a newer and better way of marking up the information than the old tag However deprecated tags still work ldquoObsoleterdquo tags may not work

bull XHTML (Extensible HTML) is still being developed It is an attempt to convert HTML into XML Version 10 has been released

Document Type Definition

bull HTML standards are defined in documents called DTDs (document type definitions) There is a default DTD used by the browser and thus we donrsquot have to explicitly define a DTD All XML documents come with a separate DTD file

bull If desired we can explicitly used a DTD by starting the HTML file with the line

ltDOCTYPE HTML PUBLIC -W3CDTD HTML 401 TransitionalENgt bull This line says that the document follows the guidelines of the World Wide

Web Consortium (W3C) transitional standards for HTML 401 ldquoTransitionalrdquo means that some HTML 32 is still involved W3C is the body that sets standards for the web

bull However you should be aware that approximately 90 of the browsers these days are Microsoftrsquos Internet Explorer This semi-monopoly allows Microsoft to ignore standards or create its own at will

bull In practical terms a web site that displays correctly for both Internet Explorer and Mozilla Firefox will probably cover just about all situations IE because of the above-stated Microsoft 900-pouind-gorilla problem and Firefox because it follows the W3C standards that all other browser use

HTML Tags

bull The basic feature in HTML documents is the ldquotagrdquo bull Tags are set off by angle brackets (ldquoltldquo and ldquogtrdquo) with the tag

name between them For example the entire HTML document is placed between the opening tag lthtmlgt and the closing tag lthtmlgt

bull Most tags occur in pairs indicating what is supposed to happen to whatever text is between them The closing tag has the same name as the opening tag but the closing tag stars with a slash () For example ltbgtmake this boldltbgt The text between the ltbgt and ltbgt tags is made boldface by the browser

bull Pairs of tags are supposed to be nested you close all inner tags before closing outer tags Thus

ltbgtltigtbold and italicizeltigtltbgt CORRECT ltbgtltigtbold and italicizeltbgtltigt WRONG

More on Tags

bull Opening tags often contain attributes as well as tag names Attributes are separated from each other by spaces and they are in the form of name=ldquovaluerdquo For example lth2 align=ldquocenterrdquogtTitlelth2gt creates a centered headline The default is left-justified

bull HTML tags are case-insensitive lttablegt ltTABLEgt and lttAbLEgt are all equivalent However the current XHTML standard suggests that we should use small letters lttablegt

bull Some tags donrsquot have a closing tag ltbrgt a line break is a common example The XHTML standard suggests putting a slash into the single tag in these cases ltbr gt

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 5: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Document Type Definition

bull HTML standards are defined in documents called DTDs (document type definitions) There is a default DTD used by the browser and thus we donrsquot have to explicitly define a DTD All XML documents come with a separate DTD file

bull If desired we can explicitly used a DTD by starting the HTML file with the line

ltDOCTYPE HTML PUBLIC -W3CDTD HTML 401 TransitionalENgt bull This line says that the document follows the guidelines of the World Wide

Web Consortium (W3C) transitional standards for HTML 401 ldquoTransitionalrdquo means that some HTML 32 is still involved W3C is the body that sets standards for the web

bull However you should be aware that approximately 90 of the browsers these days are Microsoftrsquos Internet Explorer This semi-monopoly allows Microsoft to ignore standards or create its own at will

bull In practical terms a web site that displays correctly for both Internet Explorer and Mozilla Firefox will probably cover just about all situations IE because of the above-stated Microsoft 900-pouind-gorilla problem and Firefox because it follows the W3C standards that all other browser use

HTML Tags

bull The basic feature in HTML documents is the ldquotagrdquo bull Tags are set off by angle brackets (ldquoltldquo and ldquogtrdquo) with the tag

name between them For example the entire HTML document is placed between the opening tag lthtmlgt and the closing tag lthtmlgt

bull Most tags occur in pairs indicating what is supposed to happen to whatever text is between them The closing tag has the same name as the opening tag but the closing tag stars with a slash () For example ltbgtmake this boldltbgt The text between the ltbgt and ltbgt tags is made boldface by the browser

bull Pairs of tags are supposed to be nested you close all inner tags before closing outer tags Thus

ltbgtltigtbold and italicizeltigtltbgt CORRECT ltbgtltigtbold and italicizeltbgtltigt WRONG

More on Tags

bull Opening tags often contain attributes as well as tag names Attributes are separated from each other by spaces and they are in the form of name=ldquovaluerdquo For example lth2 align=ldquocenterrdquogtTitlelth2gt creates a centered headline The default is left-justified

bull HTML tags are case-insensitive lttablegt ltTABLEgt and lttAbLEgt are all equivalent However the current XHTML standard suggests that we should use small letters lttablegt

bull Some tags donrsquot have a closing tag ltbrgt a line break is a common example The XHTML standard suggests putting a slash into the single tag in these cases ltbr gt

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 6: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

HTML Tags

bull The basic feature in HTML documents is the ldquotagrdquo bull Tags are set off by angle brackets (ldquoltldquo and ldquogtrdquo) with the tag

name between them For example the entire HTML document is placed between the opening tag lthtmlgt and the closing tag lthtmlgt

bull Most tags occur in pairs indicating what is supposed to happen to whatever text is between them The closing tag has the same name as the opening tag but the closing tag stars with a slash () For example ltbgtmake this boldltbgt The text between the ltbgt and ltbgt tags is made boldface by the browser

bull Pairs of tags are supposed to be nested you close all inner tags before closing outer tags Thus

ltbgtltigtbold and italicizeltigtltbgt CORRECT ltbgtltigtbold and italicizeltbgtltigt WRONG

More on Tags

bull Opening tags often contain attributes as well as tag names Attributes are separated from each other by spaces and they are in the form of name=ldquovaluerdquo For example lth2 align=ldquocenterrdquogtTitlelth2gt creates a centered headline The default is left-justified

bull HTML tags are case-insensitive lttablegt ltTABLEgt and lttAbLEgt are all equivalent However the current XHTML standard suggests that we should use small letters lttablegt

bull Some tags donrsquot have a closing tag ltbrgt a line break is a common example The XHTML standard suggests putting a slash into the single tag in these cases ltbr gt

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 7: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

More on Tags

bull Opening tags often contain attributes as well as tag names Attributes are separated from each other by spaces and they are in the form of name=ldquovaluerdquo For example lth2 align=ldquocenterrdquogtTitlelth2gt creates a centered headline The default is left-justified

bull HTML tags are case-insensitive lttablegt ltTABLEgt and lttAbLEgt are all equivalent However the current XHTML standard suggests that we should use small letters lttablegt

bull Some tags donrsquot have a closing tag ltbrgt a line break is a common example The XHTML standard suggests putting a slash into the single tag in these cases ltbr gt

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 8: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Character Entities

bull The other commonly seen feature in HTML documents is the ldquocharacter entityrdquo a group of characters starting with amp (ampersand) and ending with (semicolon) The entity represents a single character in the browser display

bull For example ampgt represents the gt greater than sign Since gt is part of each tag browsers have a hard time displaying the actual gt character By having ampgt in the HTML document the browser will display the character you want and not try to interpret as part of a tag

bull Very useful is ampnbsp a non-break space which is how you get multiple spaces If you just use the space bar HTML browsers will compress all those spaces into just 1 space So to get multiple spaces use several ampnbsp

bull All entity tags have a number amp62 is the same as ampgt Not all have a mnemonic name

bull All characters have entity tags but most are rarely used Thus amp97 represents the letter ldquoardquo There is no mnemonic tag for this letter mostly we just type in the letter itself

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 9: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

HTML Document Structure

bull HTML documents are supposed to have the form of a tree or equivalently in the form of a set of nested tags

bull The document should open with lthtmlgt and close with lthtmlgt

bull Within the lthtmlgt tags are 2 sections ltheadgt ltheadgt and ltbodygt ltbodygt

bull In the head section is a lttitlegt lttitlegt line The title is displayed at the very top of the browser window

bull The body section contains all the tags and text that are displayed in the main window

bull See the ldquoBasic HTML Commandsrdquo web page (httpwwwbiosniuedujohnsbioinformhtmlcomhtml )

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 10: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

A Few Tags

bull Headlines are within tags like lth1gt lth1gt H1 is the largest H6 is the smallest The ldquoalignrdquo attribute can be used to move the headline lth1 align=ldquocentergt or lth1 align=ldquorightrdquogt The default is left alignment

bull Text is set off in paragraphs within ltpgt ltpgt tags Note that the closing tag is often left off However that is a sloppy practice that I discourage

bull The ltbrgt or ltbr gt tags introduce line breaks less space between lines than with ltpgt There is no ending tag for ltbrgtrdquo it is considered part of the previous ltpgt paragraph

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 11: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Lists and Tables

bull ltulgt starts an unordered (bulleted) list ltolgt starts an ordered (numbered) list Items within the list are set off with ltligt ltligt (list item) tags

bull lttablegt starts a table lttable bordergt puts a border around it Tables are built row by row and cell by cell within each row Table rows are lttrgt lt trgt Cells within rows are lttdgt lttdgt

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 12: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Images

bull Images are placed with ltimggt tags with no closing tag The basic syntax is

ltimg src=ldquosource_filerdquo title=ldquotool tip textrdquogt

bull The src= value is a local file the path to a file in a different directory under the HTML root directory or a URL

bull The tool tip text is displayed when the mouse hovers over the image or if for some reason the image wonrsquot display It is also very useful for the visually impaired

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 13: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Links

bull To put in a hyperlink the anchor ltagt ltagt tag is used Syntax

lta href=ldquoURLrdquogttext to use as linkltagt bull You can also use an image between ltagt and

ltagt In this case clicking on the image sends you to the linked URL

bull If the linked page is on the same server you can just use the file name or the path to the file name as the URL However if the linked page is on a different server you should use the entire address including the http as the URL

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 14: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Comments

bull Anything within lt-- your comment --gt is a comment it is not displayed in the browser even though it appears in the source code

bull Comments can be many lines long

bull Note that there is no real closing tag the entire tag is enclosed within the opening lt-- --gt tag

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 15: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Forms

bull The form tag ltformgt ltformgt is used to send user-specified information back to the server The server then sends back its response a new HTML document

bull The form tag itself needs at least 2 attributes the ldquoactionrdquo attribute and the ldquomethodrdquo attribute

bull Although there are other methods we generally use method=ldquopostrdquo for our interactive programs

bull The ldquoactionrdquo of a form is the program on the server that the formrsquos contents are sent to That program processes the information and returns the response document

bull Only programs in the cgi-bin directory can be processed under our system Thus a typical form tag will look something like

ltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt form contentsltformgt

bull Note that since the program that responds to this form is on the same server the actionrsquos URL doesnrsquot need to contain ldquohttpbiolinxbiosniuedurdquo However it does need to start with ldquocgi-binrdquo

bull The form sends name=value pairs to the server ldquonamerdquo and ldquovaluerdquo are both specified within each form element

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 16: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Basic Form Elements bull All forms need a ldquoSubmitrdquo button clicking this button sends the

form to the server Syntax

ltinput type=ldquosubmitrdquo value=ldquobutton labelrdquogt If you donrsquot specify a value the button is labeled ldquoSubmitrdquo by default

bull Radio buttons You typically use them in groups all which have the same name but different values Only one button can be checked the parameter is given the value associated with the checked button It is possible to have one button checked as a default by putting the word checked after the value=par_value statement

ltinput type=ldquoradiordquo name=parameterldquo value=par_valuegt

The parameter specified by the ldquovaluerdquo attribute in the checked radio button is sent to the server

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 17: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

More Form Elements

bull Check boxes If checked the value ldquoTRUErdquo is sent to the server If not checked neither name nor value is sent to the server If you want it checked by default include the word ldquocheckedrdquo within the tag

ltinput type=ldquocheckboxrdquo name=parametergt bull Text boxes if you want to enter a single line of text

Whatever is typed into the box gets sent as a string to the program given by the form action mentioned above as the value of a parameter whose name is given by name= You can change the size of the text box with the attribute ldquosizerdquo its value is the number of characters that can be displayed

ltinput type=ldquotextrdquo name=parameterldquo size=ldquo25rdquogt

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 18: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Select Boxes

bull Select boxes a drop down list of options It has a different syntax than most of the other input tags ltselect name=rdquoparameterrdquogt ltselectgt

bull Each option in the select box is specified by the ltoptiongt ltoptiongt tag When the form is submitted the text between the opening and closing tags is sent as the value of the parameter specified in the ltselect name=ldquoparameterrdquogt tag

bull By default only 1 option is displayed You can use the size=ldquonumberrdquo attribute in the ltselectgt tag to display as many options as you want

bull To allow the user to select multiple options use the keyword ldquomultiplerdquo in the ltselectgt tag ltselect multiple name=ldquowhateverrdquogt

bull A default value is created by adding the keyword ldquoselectedrdquo to the option tag ltoption selectedgtthis oneltoptiongt

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 19: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

A Basic Form lthtmlgt ltheadgt lttitlegtBasic Formlttitlegt ltheadgt ltbodygt lth1gt Basic Formlth1gt ltpgtltform action=ldquocgi-binbios546hellocgirdquo method=ldquopostrdquogt What is your nameltinput type=ldquotextrdquo name=ldquoyour_namerdquogt ltbrgtPlease select your favorite color ltselect name=ldquocolorrdquogt ltoptiongtRedltoptiongt ltoptiongtBlueltoptiongt ltselectgt ltbr gtltinput type=ldquosubmitrdquo value=ldquoClick Megt ltformgt ltbodygt lthtmlgt

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 20: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Processing Forms

bull Once a form is submitted it is sent to a specific program on the server

bull This procedure uses the Common Gateway Interface or CGI The programs run under the CGI are called ldquoCGI scriptsrdquo We will be writing ours in Perl but other languages are also used

bull In our configuration programs that process forms must be located under the CGI root directory srvwwwhtdocsbiolinxcgi-bin You have a personal directory under this

bull For example the ldquohellocgirdquo program is located at srvwwwhtdocsbiolinxcgi-binbios546hellocgi

bull As with HTML addresses this program has an alias used as the ldquoactionrdquo attribute of the form tag ltform action=ldquohttpbiolinxbiosniueducgi-binbios546hellocgirdquo method=ldquopostrdquogt

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 21: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

CGI Basics

bull CGI programs are simply Perl programs with a few minor modifications that alter input and output

bull A key point you need to change permission on your CGI programs so that anyone can execute them When going through the Web you are the anonymous user ldquonobodyrdquo

bull Any program in your CGI directory can be run through the CGI interface (ie invoked through a form on an HTML page) I often use the ldquocgirdquo extension on my programs just to remind me that they are meant to be used on the Web

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 22: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Input to CGI Programs

bull To get input we use the CGI module Near the top of the program put in ldquouse CGIrdquo just as you would put in ldquouse strictrdquo

bull The CGI module is a complex thing that allows you to do many interesting things but I prefer to use only the simplest functions in it

bull The CGI module uses ldquoobject-orientedrdquo syntax Nothing mysterious about this it is simply an alternate way of writing things down

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 23: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Input Parameters

bull To get parameters from the form into a CGI program you first need to create a new ldquoCGI objectrdquo with the command

my $cgi_obj = new CGI

bull Then each parameter on the form needs to be captured into a Perl variable

my $var1 = $cgi_obj-gtparam(ldquoparameter1)rdquo

my $var2 = $cgi_obj-gtparam(ldquoparameter2rdquo)

bull The parameter names are the values of the ldquonamerdquo attributes in the various form elements

bull You then process the input parameters as you would any other Perl variables

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 24: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

CGI Output

bull All ldquoprintrdquo statements in programs in the cgi-bin directory have their standard output re-directed to the web server That is you send information back to the submitter of the form by simply printing it

bull One small qualification in order for your browser to understand that this is HTML you need to print the line ldquoContent-type texthtmlnnrdquo at the beginning of the printing Note the ldquonnrdquo there MUST be a blank line between the Content-type line and the lthtmlgt tag that starts the actual document

bull Otherwise all printing is exactly as we have described for other Perl programs

bull Note that you must print an HTML document to get a good display

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 25: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Multi-line Printing

bull Sometimes called a ldquohererdquo statement because you print down to ldquohererdquo

bull The statement ldquoprint ltltWZRT ldquo causes every line from that point to where ldquoWZRTrdquo appears on a line by itself to be printed with no need for ldquonrdquo or any other format commands

bull Variables are interpreted as usual

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 26: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

File Permissions

bull When you access a CGI program through a web browser you are an anonymous user with minimal permissions to do anything Even though you think you are you the owner of the program the web browser causes you to become anonymous

bull Thus you must grant execute permission on your CGI file to everyone chmod 755 programcgi

bull More complex is the problem of using a CGI program to write to another file Three things need to be done 1 Create the file you wish to write ldquotouch

srvwwwhtdocsbiolinxhtmlz123456prog_resultshtmrdquo The touch command creates the file without putting anything into it

2 Change the permissions on that file so anyone can write to it ldquochmod 666 prog_resultshtmrdquo

3 Be sure to use the full path to that file Typically the CGI file is in srvwwwhtdocsbiolinxcgi-binz123456 and you are writing an image file at srvwwwhtdocsbiolinxhtmlz123456 So in the printed output from your CGI program access the image file with a tag like ldquoltimg src=lsquohttpbiolinxbiosniueduz123456my_imagepngrsquogtrdquo

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 27: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Useful Debugging Tools

bull The CGICarp module sends error messages to your browser If you donrsquot use it you get cryptic ldquoInternal Server Errorrdquo messages with no debugging information Syntax

use CGICarp qw(fatalsToBrowser) bull on the biolinx command line perl -c ldquoyour_programcgirdquo

checks the programrsquos syntax It will either return ldquosyntax OKrdquo or an error message This allows checking the syntax without having to run the program

bull Remember that running a program through the web means that you are the anonymous user ldquonobodyrdquo who has very few privileges Be sure to check permissions especially if your program writes to any files

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 28: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Recap of CGI Processing of Forms

bull Start with an HTML file in your HTML directory srvwwwhtdocsbiolinxhtmlz012345prog1htm

bull This HTML file can be accessed through the web using a web browser at the URL

httpbiolinxbiosniueduz012345prog1htm

bull The HTML file contains a form whose action sends parameter name=ldquovaluerdquo pairs to a CGI program on the server

ltform action=ldquocgi-binz012345prog1cgirdquo method=ldquopostrdquogt

bull The CGI program ldquoprog1cgirdquo is a Perl script located in your CGI directory

srvwwwhtdocsbiolinxcgi-binz012345prog1cgi

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 29: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Recap of CGI Processing of Forms pt 2 bull Your CGI program contains the lines

use CGI

use CGICarp qw(fatalsToBrowser)

at the top just below the usrbinperl -w line

bull You must first create a new CGI object

my $cgi_obj = new CGI

bull Parameter values from the form are put into Perl variables using object-oriented syntax

my $var1 = $cgi_obj-gtparam(ldquoparameter1rdquo)

bull The Perl variables are then manipulated by the program as you see fit

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output

Page 30: Basic HTML - AOI Instituteonline.aoi.edu.au/documents/1360119648PPT3.pdfPut all your HTML documents in this directory. What is HTML •Hyper Text Markup Language is a “markup language”.

Recap of CGI Processing of Forms pt 3 bull Output is printed just as in any other Perl program except that

it is re-directed to the web browser that requested it by submitting the form

bull Output needs to have the line

Content-type texthtmlnn

at the beginning of the output


Recommended