Problem Solution Discussion Creating Forms in Scripts
18.2 Creating Forms in Scripts
18.2.1 Problem
You want t o writ e a script t hat gat hers input from a user.18.2.2 Solution
Creat e a fill- in form from wit hin your script and send it t o t he user. The script can arrange t o have it self invoked again t o process t he form s cont ent s when t he user subm it s it .18.2.3 Discussion
Web form s are a convenient way t o allow your visit ors t o subm it inform at ion, for exam ple, t o provide search keywords, a com plet ed survey result , or a response t o a quest ionnaire. Form s are also beneficial for you as a developer because t hey provide a st ruct ured way t o associat e dat a values wit h nam es by which t o refer t o t hem . A form begins and ends wit h form and form t ags. Bet ween t hose t ags, you can place ot her HTML const ruct s, including special elem ent s t hat becom e input fields in t he page t hat t he brow ser displays. The form t ag t hat begins a form should include t w o at t ribut es, action and method . The action at t ribut e t ells t he brow ser w hat t o do wit h t he form when t he user subm it s it . This will be t he URL of t he script t hat should be invoked t o process t he form s cont ent s. The method at t ribut e indicat es t o t he browser what kind of HTTP request it should use t o subm it t he form . The value will be eit her GET or POST , depending on t he t ype of request you want t he form subm ission t o generat e. The difference bet ween t hese t wo request m et hods is discussed in Recipe 18.6 ; for now, well always use POST . Most of t he form - based web script s shown in t his chapt er share som e com m on behaviors: • When first invoked, t he script generat es a form and sends it t o t he user t o be filled in. • The action at t ribut e of t he form point s back t o t he sam e script , so t hat when t he user com plet es t he form and subm it s it , t he script get s invoked again t o process t he form s cont ent s. • The script det erm ines whet her it s being invoked by a user for t he first t im e or whet her it should process a subm it t ed form by checking it s execut ion environm ent t o see what input param et ers are present . For t he init ial invocat ion, t he environm ent will cont ain none of t he param et ers nam ed in t he form . This approach isnt t he only one you can adopt , of course. One alt ernat ive is t o place a form in a st at ic HTML page and have it point t o t he script t hat processes t he form . Anot her is t o have one script generat e t he form and a second script process it . I f a form - creat ing script want s t o have it self invoked again when t he user subm it s t he form , it should det erm ine it s own pat hnam e wit hin t he web server t ree and use t hat value for t he action at t ribut e of t he opening form t ag. For exam ple, if a script is inst alled as cgi- bin m yscript in your web t ree, t he t ag can be writ t en like t his: form action=cgi-binmyscript method=POST Each API provides a way for a script t o obt ain it s own pat hnam e, so you dont have t o hardwire t he nam e int o t he script . That gives you great er lat it ude t o inst all t he script where you want . I n Perl script s, t he CGI .pm m odule provides t hree m et hods t hat are useful for creat ing form elem ent s and const ruct ing t he action at t ribut e. start_form and end_form generat e t he opening and closing form t ags, and url ret urns t he script s own pat h. Using t hese m et hods, a script can generat e a form like t his: print start_form -action = url , -method = POST; ... generate form elements here ... print end_form ; Act ually, it s unnecessary t o provide a method argum ent ; if you om it it , start_form supplies a default request m et hod of POST . I n PHP, a sim ple w ay t o get a script s pat hnam e is t o use t he PHP_SELF global variable: print form action=\PHP_SELF\ method=\POST\\n; ... generate form elements here ... print form\n; However, t hat wont work under som e configurat ions of PHP, such as when t he register_globals set t ing is disabled. [1] Anot her way t o get t he script pat h is t o access t he PHP_SELF m em ber of t he HTTP_SERVER_VARS array or as of PHP 4.1 t he _SERVER array. Unfort unat ely, checking several different sources of inform at ion is a lot of fooling around j ust t o get t he script pat hnam e in a way t hat works reliably for different versions and configurat ions of PHP, so a ut ilit y rout ine t o get t he pat h is useful. The following funct ion, get_self_path , show s how t o use _SERVER if it s available and fall back t o HTTP_SERVER_VARS or PHP_SELF ot herwise. The funct ion t hus prefers t he m ost recent ly int roduced language feat ures, but st ill works for script s running under older versions of PHP: [1] register_globals is discussed further in Recipe 18.6 . function get_self_path { global HTTP_SERVER_VARS, PHP_SELF; if isset _SERVER[PHP_SELF] val = _SERVER[PHP_SELF]; else if isset HTTP_SERVER_VARS[PHP_SELF] val = HTTP_SERVER_VARS[PHP_SELF]; else val = PHP_SELF; return val; } HTTP_SERVER_VARS and PHP_SELF are global variables, but m ust be declared as such explicit ly using t he global keyw ord if used in a non-global scope such as wit hin a funct ion . _SERVER is a superglobal array and is accessible in any scope wit hout being declared as global. The get_self_path funct ion is part of t he Cookbook_Webut ils.php library file locat ed in t he lib direct ory of t he recipes dist ribut ion. I f you inst all t hat file in a direct ory t hat PHP searches when looking for include files, a script can obt ain it s own pat hnam e and use it t o generat e a form as follows: include Cookbook_Webutils.php; self_path = get_self_path ; print form action=\self_path\ method=\POST\\n; ... generate form elements here ... print form\n; Pyt hon script s can get t he script pat hnam e by im port ing t he os m odule and accessing t he SCRIPT_NAME m em ber of t he os.environ obj ect : import os print form action=\ + os.environ[SCRIPT_NAME] + \ method=\POST\ ... generate form elements here ... print form I n JSP pages, t he request pat h is available t hrough t he im plicit request obj ect t hat t he JSP processor m akes available. Use t hat obj ect s getRequestURI m et hod as follows: form action== request.getRequestURI method=POST -- ... generate form elements here ... -- form18.2.4 See Also
Parts
» O'Reilly-MySQL.Cookbook.eBook-iNTENSiTY. 4810KB Mar 29 2010 05:03:43 AM
» Introduction Using the mysql Client Program
» Problem Solution Discussion Setting Up a MySQL User Account
» Problem Solution Discussion Starting and Terminating mysql
» Problem Solution Discussion Specifying Connection Parameters by Using Option Files
» Problem Solution Discussion Mixing Command-Line and Option File Parameters
» Problem Solution Discussion What to Do if mysql Cannot Be Found
» Problem Solution Discussion Setting Environment Variables
» Problem Solution Discussion Repeating and Editing Queries
» Problem Solution Discussion Preventing Query Output from Scrolling off the Screen
» Problem Solution Discussion Specifying Arbitrary Output Column Delimiters
» Problem Solution Discussion Logging Interactive mysql Sessions
» Discussion Using mysql as a Calculator
» Writing Shell Scripts Under Unix
» Writing Shell Scripts Under Windows
» MySQL Client Application Programming Interfaces
» Perl Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» PHP Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» Python Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» Java Connecting to the MySQL Server, Selecting a Database, and Disconnecting
» Problem Solution Discussion Checking for Errors
» Python Java Checking for Errors
» Problem Solution Discussion Writing Library Files
» Python Writing Library Files
» SQL Statement Categories Issuing Queries and Retrieving Results
» Perl Issuing Queries and Retrieving Results
» Python Issuing Queries and Retrieving Results
» Java Issuing Queries and Retrieving Results
» Problem Solution Discussion Moving Around Within a Result Set
» Problem Solution Discussion Using Prepared Statements and Placeholders in Queries
» Perl Using Prepared Statements and Placeholders in Queries
» PHP Python Java Using Prepared Statements and Placeholders in Queries
» Problem Solution Discussion Including Special Characters and NULL Values in Queries
» Perl Including Special Characters and NULL Values in Queries
» PHP Including Special Characters and NULL Values in Queries
» Python Java Including Special Characters and NULL Values in Queries
» PHP Python Java Handling NULL Values in Result Sets
» Problem Solution Discussion Writing an Object-Oriented MySQL Interface for PHP
» Class Overview Writing an Object-Oriented MySQL Interface for PHP
» Connecting and Disconnecting Writing an Object-Oriented MySQL Interface for PHP
» Error Handling Issuing Queries and Processing the Results
» Quoting and Placeholder Support
» Problem Solution Discussion Ways of Obtaining Connection Parameters
» Getting Parameters from the Command Line
» Getting Parameters from Option Files
» Conclusion and Words of Advice
» Problem Solution Discussion Avoiding Output Column Order Problems When Writing Programs
» Problem Solution Discussion Using Column Aliases to Make Programs Easier to Write
» Problem Solution Discussion Selecting a Result Set into an Existing Table
» Problem Solution Discussion Creating a Destination Table on the Fly from a Result Set
» Problem Solution Discussion Moving Records Between Tables Safely
» Problem Solution Discussion Cloning a Table Exactly
» Problem Solution Discussion Generating Unique Table Names
» Problem Solution Discussion Using TIMESTAMP Values
» Problem Solution Discussion Using ORDER BY to Sort Query Results
» Solution Discussion Working with Per-Group and Overall Summary Values Simultaneously
» Problem Solution Discussion Changing a Column Definition or Name
» Problem Solution Discussion Changing a Table Type
» Problem Solution Discussion Adding Indexes
» Introduction Obtaining and Using Metadata
» Problem Solution Discussion Perl PHP
» Problem Solution Discussion Perl
» PHP Obtaining Result Set Metadata
» Python Obtaining Result Set Metadata
» Java Obtaining Result Set Metadata
» Using Result Set Metadata to Get Table Structure
» Problem Solution Discussion Database-Independent Methods of Obtaining Table Information
» Problem Solution Discussion Displaying Column Lists Interactive Record Editing
» Mapping Column Types onto Web Page Elements Adding Elements to ENUM or SET Column Definitions
» Selecting All Except Certain Columns
» Problem Solution Discussion Listing Tables and Databases
» Problem Solution Writing Applications That Adapt to the MySQL Server Version
» Discussion Writing Applications That Adapt to the MySQL Server Version
» Problem Solution Discussion Determining Which Table Types the Server Supports
» General Import and Export Issues
» Problem Solution Discussion Importing Data with LOAD DATA and mysqlimport
» Problem Solution Discussion Specifying the Datafile Location
» Problem Solution Discussion Specifying the Datafile Format
» Problem Solution Discussion Dealing with Quotes and Special Characters
» Problem Solution Discussion Handling Duplicate Index Values
» Problem Solution Discussion Getting LOAD DATA to Cough Up More Information
» Problem Solution Discussion Dont Assume LOAD DATA Knows More than It Does
» Problem Solution Discussion Skipping Datafile Columns
» Problem Solution Discussion Exporting Query Results from MySQL
» Using the mysql Client to Export Data
» Problem Solution Discussion Exporting Tables as Raw Data
» Problem Solution Discussion Exporting Table Contents or Definitions in SQL Format
» Problem Solution Discussion Copying Tables or Databases to Another Server
» Problem Solution Discussion Writing Your Own Export Programs
» Problem Solution Discussion Converting Datafiles from One Format to Another
» Problem Solution Discussion Extracting and Rearranging Datafile Columns
» Problem Solution Discussion Validating and Transforming Data
» Writing an Input-Processing Loop Putting Common Tests in Libraries
» Problem Solution Discussion Validation by Pattern Matching
» Problem Solution Discussion Using Patterns to Match Numeric Values
» Problem Solution Discussion Using Patterns to Match Dates or Times
» See Also Using Patterns to Match Dates or Times
» Problem Solution Discussion Using Patterns to Match Email Addresses and URLs
» Problem Solution Discussion Validation Using Table Metadata
» Problem Solution Discussion Issue Individual Queries Construct a Hash from the Entire Lookup Table
» Use a Hash as a Cache of Already-Seen Lookup Values
» Problem Solution Discussion Converting Two-Digit Year Values to Four-Digit Form
» Problem Solution Discussion Performing Validity Checking on Date or Time Subparts
» Problem Solution Discussion Writing Date-Processing Utilities
» Problem Solution Discussion Performing Date Conversion Using SQL
» Problem Solution Discussion Guessing Table Structure from a Datafile
» Problem Solution Discussion A LOAD DATA Diagnostic Utility
» Problem Solution Discussion Exchanging Data Between MySQL and Microsoft Access
» Problem Solution Discussion Exchanging Data Between MySQL and Microsoft Excel
» Problem Solution Discussion Exchanging Data Between MySQL and FileMaker Pro
» Problem Solution Discussion Importing XML into MySQL
» Epilog Importing and Exporting Data
» Introduction Generating and Using Sequences
» Problem Solution Discussion Using AUTO_INCREMENT To Set Up a Sequence Column
» Problem Solution Discussion Choosing the Type for a Sequence Column
» Problem Solution Discussion Ensuring That Rows Are Renumbered in a Particular Order
» Problem Solution Discussion Managing Multiple Simultaneous AUTO_INCREMENT Values
» Problem Solution Discussion Using AUTO_INCREMENT Values to Relate Tables
» Problem Solution Discussion Generating Repeating Sequences
» Problem Solution Discussion See Also
» Performing a Related-Table Update Using Table Replacement
» Performing a Related-Table Update by Writing a Program
» Performing a Multiple-Table Delete by Writing a Program
» Problem Solution Discussion Dealing with Duplicates at Record-Creation Time
» Problem Solution Discussion Using Transactions in Perl Programs
» Problem Solution Discussion Using Transactions in Java Programs
» Problem Solution Discussion Using Alternatives to Transactions
» Grouping Statements Using Locks
» Rewriting Queries to Avoid Transactions
» Introduction Introduction to MySQL on the Web
» Problem Solution Discussion Basic Web Page Generation
» Problem Solution Discussion Using Apache to Run Web Scripts
» Problem Solution Discussion Using Tomcat to Run Web Scripts
» Installing the mcb Application
» Installing the JSTL Distribution
» Problem Solution Discussion Encoding Special Characters in Web Output
» General Encoding Principles Encoding Special Characters in Web Output
» Encoding Special Characters Using Web APIs
» Introduction Incorporating Query Results into Web Pages
» Problem Solution Discussion Creating a Navigation Index from Database Content
» Creating a Multiple-Page Navigation Index
» Problem Solution Discussion Storing Images or Other Binary Data
» Storing Images with LOAD_FILE Storing Images Using a Script
» Problem Solution Discussion Retrieving Images or Other Binary Data
» Problem Solution Discussion Serving Banner Ads
» Problem Solution Discussion Serving Query Results for Download
» Introduction Processing Web Input with MySQL
» Problem Solution Discussion Creating Forms in Scripts
» Problem Solution Discussion Creating Multiple-Pick Form Elements from Database Content
» Problem Solution Discussion Loading a Database Record into a Form
» Problem Solution Discussion Collecting Web Input
» Web Input Extraction Conventions Perl
» Problem Solution Discussion Validating Web Input
» Problem Solution Discussion Using Web Input to Construct Queries
» Problem Solution Discussion Processing File Uploads
» Perl Processing File Uploads
» Problem Solution Discussion Performing Searches and Presenting the Results
» Problem Solution Discussion Generating Previous-Page and Next-Page Links
» Paged Displays with Previous-Page and Next-Page Links
» Paged Displays with Links to Each Page
» Problem Solution Discussion Web Page Access Counting
» Problem Solution Discussion Web Page Access Logging
» Problem Solution Discussion Setting Up Database Logging
» Other Logging Issues Using MySQL for Apache Logging
» Session Management Issues Introduction
» Problem Solution Discussion Installing Apache::Session
» The Apache::Session Interface
» A Sample Application Using MySQL-Based Sessions in Perl Applications
» Problem Solution Discussion The PHP 4 Session Management Interface
» Specifying a User-Defined Storage Module
» Problem Solution Discussion Using MySQL for Session BackingStore with Tomcat
» The Servlet and JSP Session Interface A Sample JSP Session Application
Show more