 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
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?
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
CGI Programming.
Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai.
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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 6 – Introduction to the Common Gateway Interface (CGI) Outline 6.1 Introduction 6.2 Client and Web.
CS 898N – Advanced World Wide Web Technologies Lecture 11: Internet Database Programming Chin-Chih Chang
 2008 Pearson Education, Inc. All rights reserved. 1 Introduction to HTML.
CGI Programming in Perl Software Tools. Lecture 22 / Slide 2 CGI Programming l Last time we looked at designing a static web page. Today we will see how.
Defined/Undef my $i; if( defined $i ) #false $i=0; if( defined $i ) #true my %hash; #or %hash=(); defined %hash; #false, hash is empty $hash{“1”}=“one”;
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.
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.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
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 ‘Dynamic’ Web Pages So far, we have developed ‘static’ web-pages, e.g., cv.html, repair.html and order.html. There is often a requirement to produce.
JAVASCRIPT HOW TO PROGRAM -2 DR. JOHN P. ABRAHAM UTPA.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
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.
CO1552 Web Application Development HTML Forms. Websites can be made more interactive by providing facilities for users to provide data To get user entered.
INTERNET APPLICATION DEVELOPMENT For More visit:
Forms and Form Controls Chapter What is a Form?
 2004 Prentice Hall, Inc. All rights reserved. Chapter 35 – Python Outline 35.1 Introduction First Python Program Python Keywords 35.2 Basic.
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
CP476 Internet Computing CGI1 CGI is a common way to provide for specific computations on server side, interactions with users, or access to databases.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
USING PERL FOR CGI PROGRAMMING
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Website Development with PHP and MySQL Saving Data.
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.
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Introduction to Programming the WWW I CMSC Winter 2003.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 17 - Web Automation and Networking Outline 17.1Introduction 17.2Introduction to LPW 17.3 LPW Commands.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Adapted from  2012 Prentice Hall, Inc. All rights reserved. 5 th ed: Chapter 2 and th ed: 4.11 SY306 Web and Databases for Cyber Operations.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
1 CGI (Common Gateway Interface) CmpE 587 Emir Bayraktar Onur Bük.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
How to Write Web Forms By Mimi Opkins.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Chapter 5 Introduction to XHTML: Part 2
CGI Programming Part II UNIX Security
Presentation transcript:

 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction 7.3A Simple CGI Script 7.4Using CGI.pm to Generate HTML 7.5Sending Input to a CGI Script 7.6Using HTML Forms to Send Input 7.7Using CGI.pm to Create forms and Read Input 7.8Other Headers 7.9Example: An Interactive Portal 7.10Internet and World Wide Web Resources

 2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig 7.2: fig07_02.pl 3# Displays the current date and time in a Web browser. 4 5use warnings; 6use strict; 7 8print "Content-type: text/html\n\n"; 9print " Current date and time "; 10print " \n "; 11print scalar( localtime() ); 12print " "; The use warnings statement warns the user of possible typos, the use of uninitialized variables and other potential problems in the code. The statement use strict forces the programmer to declare all variables as package variables or lexically scoped variables. Until now, the output of print has always been displayed on the screen. However, technically speaking, the default target for print is standard output. When a Perl program is executed as a CGI script, the standard output is redirected to the client Web browser. Built-in function localtime, when evaluated in scalar context, returns a string which, in this case, is output directly to the screen by print. HTTP header.

 2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig. 7.4: fig07_04.pl 3# Program to display CGI environment variables. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9print header(), start_html( "Environment Variables" ); 10 11print ' '; 12 13foreach my $variable ( sort( keys %ENV ) ) { 14 print Tr( td( b( "$variable:" ) ), 15 td( i( $ENV{ $variable } ) ) ); 16} 17 18print ' ', end_html(); The use statement directs the Perl interpreter to include a set of functions from the CGI.pm module. This standard function set is designated by the import tag :standard. Directs the Perl program to print the HTTP header using function header from the CGI library. Function start_html returns a string containing the standard opening tags of an HTML document (,,, etc., up to the opening tag). When the function is called with a single argument, the argument is embedded between the tags of the returned string to give the Web page a name, which is displayed in the browser’s title bar. Begins the HTML table in which the data will be displayed. The %ENV hash is a built-in hashtable in Perl that contains the names and values of all the environment variables. The foreach loop iterates through each of the keys of %ENV, sort ing them lexically first. Function Tr returns a string containing the opening (table row) tag and the closing tag to create a table row. Function Tr takes two arguments, each of which represent one cell in the table row; thus, the table is output with two columns. The first argument calls function td to generate the opening and closing (table data) tags around each cell of the table column. Each cell contains the output of the b function, which generates (bold) tags around text to be output in bold type. The actual text to be output in the cell is simply the name of the environment variable, followed by a colon ( "$variable:" ). In the second argument we output the value of the environment variable, $ENV { $variable }, italicized (with tags generated by function i within a table cell (using td ). We close the table and call end_html to generate the final and tags.

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig. 7.5: fig07_05.pl 3# An example of using QUERY_STRING. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9my $query = $ENV{ "QUERY_STRING" }; 10 11print header(), start_html( "QUERY_STRING example" ); 12print h2( "Name/Value Pairs" ); 13 14if ( $query eq "" ) { 15 print 'Please add some name-value pairs to the URL above. '; 16 print 'Or try this.'; 17} 18else { 19 print i( "The query string is '$query'." ), br(); = split( "&", $query ); foreach my $pair ) { 24 my ( $name, $value ) = split( "=", $pair ); 25 print "You set '$name' to value '$value'.", br(); 26 } 27} 28 29print end_html(); The QUERY_STRING variable contains extra information that is appended to a URL in a GET request, following a question mark ( ? ). The value of QUERY_STRING is placed into variable $query. We test to see if $query is empty. If so, we print a message instructing the user to add a query string to the URL. To break apart each of these resulting name– value pairs, we use a foreach loop that iterates through each $pair in turn. We call split to break the pair at the equals sign into a $name and a $value.

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Fig. 7.6 HTML form elements.

 2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig 7.7: fig07_07.pl 3# Demonstrates GET method with HTML form. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9our ( $name, $value ) = split( '=', $ENV{ QUERY_STRING } ); 10 11print header(), start_html( 'Using GET with forms' ); 12print p( 'Enter one of your favorite words here: ' ); 13 14print ' '; 15print ' '; 16print ' '; 17print ' '; 18 19if ( $name eq 'word' ) { 20 print p( 'Your word is: ', b( $value ) ); 21} 22 23print end_html(); The form is output with the tags. Notice that the method attribute is GET and the action attribute is the name of a file, in this case the file is fig07_07.pl. The form contains two input fields. The first is a single-line text field ( type = "text" ) with the name word. The second displays a button, labeled Submit word, to submit the form data. During the second execution of the script, when the query string is decoded, value word is assigned to variable $name and the user’s favorite word is assigned to variable $value. Also during the second execution of the script, the conditional statement is true and the word outputs to the screen.

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl2# Fig 7.8: fig07_08.pl 3# Demonstrates POST method with HTML form. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9our ( $data, $name, $value ); 10 11read( STDIN, $data, $ENV{ 'CONTENT_LENGTH' } ); 12( $name, $value ) = split( '=', $data ); 13 14print header(), start_html( 'Using POST with forms' ); 15print p( 'Enter one of your favorite words here: ' ); 16 17print ' '; 18print ' '; 19print ' '; 20print ' '; 21 22if ( $name eq 'word' ) { 23 print p( 'Your word is: ', b( $value ) ); 24} 25 26print end_html(); The POST method sets the environment variable CONTENT_LENGTH, to indicate the number of characters of data that were sent (or posted). Function read is used to read in exactly that many characters from standard input ( STDIN ) and store the characters in the $data variable.

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig 7.9: fig07_09.pl 3# Demonstrates use of CGI.pm with HTML form. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9my $word = param( "word" ); 10 11print header(), start_html( 'Using CGI.pm with forms' ); 12print p( 'Enter one of your favorite words here: ' ); 13 14print start_form(), textfield( "word" ); 15print submit( "Submit word" ), end_form(); 16 17print p( 'Your word is: ', b( $word ) ) if $word; 18print end_html(); Function param takes one argument: the name of an HTML form field. If form data containing a name–value pair with the given name (in this case, word ) were submitted, then the name’s associated value would be returned. Otherwise, param returns undef. Function start_form generates the opening tag. When no arguments are given, start_form defaults to the POST method. Function textfield generates HTML to create an single-line form element with the name word. Function submit creates a submit button with the given argument ( Submit word ) as a label. Function end_form prints the closing form tag,. We use an if structure to execute the print statement only if $word was assigned a value from function param.

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Outline Enter here Welcome to Deitel Travel! 8 9<form method = "POST" 10 action = " Please enter your name: Members, please enter the password: Note that password is not encrypted Field to collect client’s name. Field to collect member’s password.

 2001 Prentice Hall, Inc. All rights reserved. Outline

 2001 Prentice Hall, Inc. All rights reserved. Outline 1#!perl 2# Fig. 7.11: fig07_11.pl 3# Handles entry to Deitel Travel 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9print redirect( "/fig07_10.html" ) unless param( "name" ); 10 11my $name = param( "name" ); 12my $password = param( "password" ); 13 14print header(), start_html( "Deitel Travel" ), 15 h1( "Welcome, $name!" ); 16 17print "Here are our weekly specials:", br(), 18 ul( li( "Boston to Taiwan for \$300" ) ); 19 20if ( $password eq "Coast2coast" ) { 21 print hr(), "Current specials just for members:", br(), 22 ul( li( "San Diego to Hong Kong for \$250" ) ); 23} 24elsif ( $password ne "" ) { 25 print i( "Sorry, you entered the wrong password.", 26 "If you have the correct password, enter", 27 "it to see more specials." ); 28} Checks if any data were posted for the name field using function param. Unless data have been posted, the client is redirected to the portal page We assign the form-field values to variables $name and $password. We use $name to print a personal greeting for the client. The current weekly specials are displayed. If the member password is correct, additional specials are output. If the password is not correct and the client submitted anything other than the empty string, the client is informed that the password was invalid.

 2001 Prentice Hall, Inc. All rights reserved. Outline 29else { 30 print i( "Become a member today for more great deals!" ); 31} 32 33print hr(), end_html(); If no password was entered, a message promoting the benefits of membership is output.

 2001 Prentice Hall, Inc. All rights reserved. Outline