Problem Solution Discussion Creating Forms in Scripts

form s t o edit exist ing records. For t hat t ype of act ivit y, you m ust be able t o t ell which record t o updat e, which is difficult wit hout a unique record ident ifier. The list of available colors is m aint ained in a separat e t able, cow_color : CREATE TABLE cow_color color CHAR20 ; For purposes of illust rat ion, assum e t hat t he color t able looks like t his: +---------------+ | color | +---------------+ | Black | | Black White | | Brown | | Cream | | Red | | Red White | | See-Through | +---------------+ An applicat ion can use t hese t ables t o generat e list elem ent s in an order ent ry form , m aking it unnecessary for t he applicat ion t o have a lot of specialized built - in knowledge about t he available opt ions. The next several sect ions describe how t o do t his, and how t o process t he input t hat you obt ain when a user subm it s a form .

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 ... -- form

18.2.4 See Also