CGI Programming. What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept. Almost.

Slides:



Advertisements
Similar presentations
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Advertisements

CGI Programming.
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
Tutorial 6 Working with Web Forms
More CGI Programming Multiple Submits Cookies ing File Uploading.
CS 898N – Advanced World Wide Web Technologies Lecture 6: PERL and CGI Chin-Chih Chang
CGI programming in Perl Learning Objectives: 1. To understand how a CGI program works in Perl and how to make it runnable in web browsers 2. To learn how.
Python and Web Programming
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Session 2 Tables and forms in HTML Adapted by Sophie Peter from original document by Charlie Foulkes.
CGI. CGI Programming What is "CGI"? –Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Creating Web Page Forms
Tutorial 6 Forms Section A - Working with Forms in JavaScript.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
CST JavaScript Validating Form Data with JavaScript.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
XP Tutorial 6New Perspectives on HTML and XHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Forms and Form Controls Chapter What is a Form?
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Internet / Intranet Fall 2000 Class 7. Brandeis University Internet/Intranet Spring Class 7 Agenda Project / Homework Discussion Forms Validating.
Internet / Intranet CIS-536 Class 7. 2 HTML Forms A Method to Allow Users to Pass Information to a CGI Script Forms Allow Information to Be Entered Via:
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
HTML II. Factors to consider in designing a website. Organizing your files. HTML Tables. Unordered Lists. Ordered Lists. HTML Forms. Learning Objectives.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
CP476 Internet Computing CGI1 CGI is a common way to provide for specific computations on server side, interactions with users, or access to databases.
Python CGI programming
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
USING PERL FOR CGI PROGRAMMING
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
Website Development with PHP and MySQL Saving Data.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
1 HTML Forms
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
ITCS373: Internet Technology Lecture 5: More HTML.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
HTLM Forms CS3505. Form Handling in Browser html User Files out form WEbBROWSErWEbBROWSEr User read response submit Get URL?input html Get file html script.
+ FORMS HTML forms are used to pass data to a server. begins and ends a form Forms are made up of input elements Every input element has a name and value.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Creating Web Page Forms COE 201- Computer Proficiency.
Netprog CGI and Forms1 CGI and Forms A detailed look at HTML forms.
HTML Forms.
Week 10: HTML Forms HNDIT11062 – Web Development.
HTML Forms. A form is simply an area that can contain form fields. Form fields are objects that allow the visitor to enter information - for example text.
COS 125 DAY 17. Agenda  Assignment 7 not finished grading  Assignment 8 posted Due April 9  Capstone progress reports due  Quiz 2 Corrected 2 A’s,
Copyright (c) 2004 Prentice-Hall. All rights reserved. 1 Committed to Shaping the Next Generation of IT Experts. Project 6: Creating XHTML Forms Kelly.
1 HTML forms (cont.)
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Ashima Wadhwa Java Script And Forms. Introduction Forms: –One of the most common Web page elements used with JavaScript –Typical forms you may encounter.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
Introducing Forms.
Presentation transcript:

CGI Programming

What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept. Almost any language can produce CGI programs –even C++ (gasp!!) However, Perl does have a *very* nice interface to creating CGI methods

How Does it Work? A program is created, like normal. The program must be made user-executable –#!/usr/bin/env perl –chmod u+x filename.cgi A user visits a web page. The web browser contacts the server where the CGI program resides, and asks it to run the program –passes Parameters to the program similar to command line arguments The server runs the program with the parameters passed in The server takes the output of the program and returns it to the web browser. The web browser displays the output as an HTML page –or text, or jpeg, or whatever

Forms Most (not all) CGI scripts are contacted through the use of HTML forms. A form is an area of a web page in which the user can enter data, and have that data submitted to another page. When user hits a submit button on the form, the web browser contacts the script specified in the form tag.

Creating a Form … Method attribute specifies how parameters are passed to the CGI program. "post" means they’re passed in the HTTP header (and therefore aren’t seen anywhere). "get" means they’re passed through the query string of the URL itself, and therefore seen in the address bar in the web browser. Action attribute specifies which program you want the web browser to contact. is a tag used to accept User data. type="submit" specifies a Submit button. When user clicks this button, browser contacts file specified in action attribute.

Form Input Types Many different ways of getting data from user. Most specified by tag, type specified by type attribute text  a text box checkbox  a check box radio  a Radio button password  password field –(text box, characters display as ******) hidden  hidden field (nothing displayed in browser) submit  Submit button. Submits the form reset  Reset button. Clears form of all data. button  A button the user can press –(usually used w/ javaScript. *shudder*) file  field to upload a file image  an image user can click to submit form

Other Attributes of name  name of input field. value  value returned from checks & radios; text of submits and buttons; contents of text, password, and hidden size  width of text or password checked  radio or checkbox ‘turned on’ src  URL of an image

Inputs that Don’t Use... – Multi-line text field. Specify rows and cols attributes, and type any pre-existing text between the tags.... –create a drop-down menu. –... Options in the drop down menu. value attribute is the value of the select input when submitted; text between tags is what is displayed in the browser for this option

Great. We can input. Now what? Now, we can write a CGI program that takes those inputs, executes some code, and returns an HTML page As with all perl scripts, first line of the program MUST be the shebang: #!/usr/bin/env perl Next, need to include all the CGI methods. These are stored in CGI.pm –functions we care about stored in the 'standard' tag from %EXPORT_TAGS use CGI ':standard';

Outputting from CGI Just as CGI program took ‘input’ from user via web browser, it outputs back to user via web browser as well. STDOUT is redirected to the web browser that contacted it. This means you don’t have to learn any new output functions. print() will now throw data to the web browser.

Beginning a CGI Program #!/usr/bin/env perl use strict; use warnings; use CGI qw/:standard/; print header('text/html'); header() prints the HTTP header to web browser. The argument is the MIME type of the forthcoming data. –Defaults to text/html, so you can usually just leave the argument out.

Now Create your Output Remember, you’re building an HTML page in the output. So you must follow the HTML format: print " \n \n My CGI Program \n \n \n"; CGI.pm gives you a better way to do this. We’ll get to it soon.

Where’d Those Inputs Go? They were passed into the CGI program as parameters. You can retrieve them using the param() function. Called in scalar context without any arguments, returns the number of parameters passed to the script –my $num_params = param(); Called in list context without an argument, returns names of all parameters. = param(); Called in scalar context, takes name of one parameter, and returns value of that parameter –my $name = param('name'); Called in list context w/ an argument, returns array of all values for that parameter (ex, for checks and dropdowns) = param('color');

Dump subroutine defined in CGI.pm. Retrieves list of parameters and creates an HTML list of all parameters and all values. Like most CGI functions, doesn’t print anything. You must manually print the return value of the function call. print Dump;

HTML Shortcuts CGI.pm gives you methods to create HTML code without actually writing HTML. most HTML tags are aliased to CGI functions. unpaired tags: –print br(); # sends to browser paired tags: –print strong('Bold text'); # Bold text –print em('Italic'); # Italic

More shortcuts For tags with attributes, place name/value attributes in a hash reference as the first argument. The string enclosed in tag is the second argument. Ex: print a( {href=>'sample.html', target=>'top'}, 'Sample file' ); Produces: Sample file You may think this is needless amounts of extra learning, with no time or length benefits –You’re probably right.

start_html() and end_html() start_html() can take one parameter, the title. print start_html("My title"); – My title Can also take named parameters, with attributes to give to tag: print start_html (-title=>'My Title', -bgcolor=>'Red'); – My title print end_html; –

HTML Form Shortcuts For full list of Form shortcuts, see "FORM ELEMENTS" in perldoc CGI –or Each one takes parameters as name/value pairs. Name starts with a dash. Most parameters are optional print startform( -method=> 'post', -action=> 'foo.cgi' ); –default method is post. default action is current script produces: print endform(); # produces

Input Shortcuts textfield(-name=>"MyText", -default =>"This is a text box") – All HTML Form input shortcuts are similar. Again, see perldoc CGI for full list and description. –

Programmer Beware "default" in input methods is value used *first* time script is loaded only. After that, they hold the values they had the last time the script was run. to override (ie, force default value to appear), set parameter -override=>1 inside input method: textfield(-name=>"foo", -default =>"bar", -override=>1);

Avoid Conflicts Some HTML tags have same name as internal Perl functions. Perl will get very confused if you try to use the CGI shortcut methods to print these tags… – table row. conflicts with tr/// –use Tr() or TR() instead – dropdown list. conflicts with select(). –use Select() instead - pass parameters to Java applet – conflicts with param(). –use Param() instead - Make text subscripted. Conflicts with sub keyword. –Use Sub() instead

All in One file One common method of CGI programming is to make both the form and the response all in one script. Here’s how you do that… #!/usr/bin/env perl use strict; use warnings; use CGI qw/:standard/; print header; if (!param()){ #no parameters print start_html("Here’s a form"); print startform; #create your form here… print endform; } else { #parameters passed, form submitted print start_html("Here’s the result"); #display the results of the submitting form }

What button was pushed? If you want to determine which button the user pushed to activate the CGI script, check the parameter list. Only the button that was pushed will be in the parameter list. Other buttons do not exist. if (param('btn1')) { print "btn1 clicked" } if (param('btn2')) { print "btn2 clicked" } if(param('btn') eq 'One') { print "One Clicked" } if(param('btn') eq 'Two') { print "Two Clicked" }

CGI on CS The CGI-enabled server is cgi2.cs.rpi.edu To run your scripts on cgi2.cs –set shebang: #!/usr/bin/env perl –make file user-executable –put the file in public.html/cgi-bin directory –Make sure public.html and cgi-bin are world-executable –go to –see "Steps For CGI" in handouts for detailed process If all goes well, your script’s output will be displayed. If all does not go well, you’ll get HTTP 500

Debugging Debugging a CGI program can be a very frustrating task. If there are any compilation errors whatsoever, the web browser will simply display 500 Internal Server Error It will also tell you that "more information may be found in the server logs". Those server logs are located on the CS System at: /servers/cgi2/logs/error.log There is, of course, a nicer alternative…

CGI::Carp Enables you to view your compilation errors in the browser, rather than searching for them in the server's error logs. #!/usr/bin/env perl use strict; use warnings; use CGI qw/:standard/; use CGI::Carp ( 'warningsToBrowser', 'fatalsToBrowser' ); print header; warningsToBrowser(1); fatal errors will now be displayed in the browser. Warnings will be displayed as HTML comments (view the source of your page to see them)

Debugging process If you get HTTP 500, even with using CGI::Carp... Try running your script from the command line –ssh into cgi2.cs.rpi.edu, change to your public.html/cgi- bin directory, and run your script Run it *directly*:./prog.cgi, not perl prog.cgi If that appears to work without problems, then check the server logs –try grep 'ing for your username or filename: grep lallip error.log grep my_cgi.cgi error.log

One Common Error /usr/bin/env: No such file or directory "bad interpreter" You wrote your program in Windows, and transferred it to Unix via some binary mode process. You have Windows-style newlines. The server is actually looking for a program named "perl\r" to execute. To fix: dos2unix file.pl file.pl –but really, just stop writing your stuff in Windows!