TÉCNICAS DE GESTÃO DE BASE DE DADOS 12º ano CTI Actividades de Laboratório 1

Introd !"o # $%$

Antes de iniciares a resolução dos seguintes exercícios, deves ser capaz de: ☑ Compreender e aplicar as tags e elementos fundamentais do HTML, incluindo listas, formatação de texto, iperligaç!es, ta"elas, formul#rios e "ot!es$ ☑ Aplica Cascading %t&le % eets 'C%%( para formatar elementos de p#ginas mais comuns$ ☑ Compreender conceitos "#sicos de programação, nomeadamente, os de vari#vel, função e vector$ ☑ )tilizar estruturas de decisão para resolver pro"lemas concretos$

A resolução dos exercícios a seguir apresentados exigem a correcta instalação e configuração de *H* + ou superior$ Todos os exercícios foram testados em *H* v,$

&ndice
-xercício .$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/ -xercício 0$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+ -xercício /$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$, -xercício +$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$1 -xercício ,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$2 -xercício 1$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$3 Local and 4lo"al 5aria"les$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$3 *re67efined 5aria"les$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$8 -xercício 2$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.9 -xercício 3$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.. -xercício 8$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.0 -xercício .9$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.+ -xercício ..$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.1 -xercício .0$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$.3 -xercício ./$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$09 Creating a Calculation :orm$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$09 Creating t e Calculation %cript$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$0. %u"mitting ;our :orm and 4etting <esults$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$00 -xercício .+$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$0+ <etrieving and using <-M=T->A77<$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$0+ <etrieving and using HTT*>)%-<>A4-?T$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$0, -xercício .,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$01 -xercício .1$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$03 -xercício .2$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/9 Creating an @nput :orm$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/9 Creating a %cript to 7ispla& :orm 5alues$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/. %u"mitting ;our :orm and 4etting <esults$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/0 -xercício .3$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// Creating a <edirection :orm$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// Creating t e <edirection %cript and Testing @t$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/+ -xercício .8$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/, Creating t e :eed"acA :orm$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/, Creating a %cript to Mail ;our :orm$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/1 %u"mitting ;our :orm and 4etting <esults$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/2 -xercício 09$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/8 Creating t e @nitial %cript$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/8 Adding -rror C ecAing to t e %cript$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+9 %u"mitting ;our :orm and 4etting <esults$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+0 %aving t e 5alues if ;ou MaAe an -rror$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$+/

Exercício 1
="Bectivo: – @dentificar exemplos de aplicação de tags de início e fim de instruç!es *H*$ – <eproduzir conBunto de tags necess#rias para escrever instruç!es *H*$

. $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing code, C ic uses t e first tag t&pe: DEp p ec o FD*GT is is a test using t e first tag t&pe$DH*GFI EG / $ T&pe t e folloCing code, C ic uses t e second tag t&pe: DE ec o FD*GT is is a test using t e second tag t&pe$DH*GFI EG + $ T&pe t e folloCing code, C ic uses t e t ird tag t&pe: Dscript languageJFp pFG ec o FD*GT is is a test using t e t ird tag t&pe$DH*GFI DHscriptG , $ %ave t e file Cit t e name p ptags$p p$ 1 $ *lace t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe

ttp:HH.02$9$9$.Hp ptags$p p$

?ote: K ile executing t e examples in t is "ooA, if &ou are using *H* on an external Ce" server, su"stitute t at serverLs domain name for t e .02$9$9$. address in t e )<L$ @n &our Ce" "roCser, &ou s ould see t e results of &our script$

@ntrodução M *H*

*#gina / de +/

Exercício 2
="Bectivos: – @ntegrar cNdigo HTML com cNdigo *H*$ – @dentificar tag em *H* Oue possi"ilita imprimir contePdo$

. $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-GM& :irst *H* %criptDHT@TL-G DHH-A7G DQ=7;G / $ T&pe t e folloCing *H* code: DE ec o FD*GDemGHello KorldR @Lm using *H*RDHemGDH*GFI EG + $ Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG , $ %ave t e file Cit t e name firstscript$p p$ 1 $ *lace t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hfirstscript$p p$ 3 $ @n &our Ce" "roCser, &ou s ould see t e results of &our script$ 8 $ @n &our Ce" "roCser, vieC t e source of t is document$ ?otice t at t e HTML source contains onl& HTML code, C ic is correct "ecause t is "locA of *H* Cas executed: DE ec o FD*GDemGHello KorldR @Lm using *H*RDHemGDH*GFI EG T is "locA contains t ree elements: t e command 'ec o(, t e string 'D*GDemGHello KorldR @Lm using *H*RDHemGDH*G(, and t e instruction terminator 'I($:amiliarize &ourself noC Cit ec o, "ecause it Cill liAel& "e &our most often6used command$ T e ec o statement is used to output informationSin t is case, to print t is HTML output: D*GDemGHello KorldR @Lm using *H*RDHemGDH*G

@ntrodução M *H*

*#gina + de +/

Exercício 3
="Bectivos: – @dentificar exemplos de instruç!es *H*$ – <econ ecer a sintaxe de uma instrução *H*$ – Corrigir erros de sintaxe de instruç!es *H*$

. $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-GMaAing an -rrorDHT@TL-G DHH-A7G DQ=7;G / $ T&pe t e folloCing *H* code: DE ec o FD*G@ am tr&ing to produce an errorDH*GF ec o FD*GKas @ successfulEDH*GFI EG + $ Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG , $ %ave t e file Cit t e name errorscript$p p$ 1 $ *lace t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Herrorscript$p p$ K at a nast& errorR T e error message sa&s t at t e error is on line 3$ TaAe a looA at lines 2 and 3 of t e script: ec o FD*G@ am tr&ing to produce an errorDH*GF ec o FD*GKas @ successfulEDH*GFI K atLs t e pro"lemE

@ntrodução M *H*

*#gina , de +/

Exercício 4
="Bectivos: – <econ ecer a função das aspas nas instruç!es *H*$ – )tilizar aspas com diversas finalidades nas instruç!es *H*$ – @dentificar função da T '"arra esOuerda($

. $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-GTr&ing :or Anot er -rrorDHT@TL-G DHH-A7G DQ=7;G / $ T&pe t e folloCing *H* code: DE ec o FD*G@ t inA t is is reall& FcoolFRDH*GFI EG + $ Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG , $ %ave t e file Cit t e name errorscript0$p p$ 1 $ *lace t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Herrorscript0$p p Anot er parse errorR TaAe a looA at t e *H* code: ec o FD*G@ t inA t is is reall& FcoolFRDH*GFI K atLs t e pro"lemE Qecause &ou ave a set of Ouotation marAs Cit in anot er set of Ouotation marAs, t at inner set as to "e escaped$ T is error also as a simple fix: 3 $ =pen t e errorscript0$p p file$=n line 8, escape t e inner Ouotation marAs "& placing a "acAslas "efore eac one: ec o FD*G@ t inA t is is reall& TFcoolTFRDH*GFI 8 $ %ave t e file$ .9 $*lace t is file in t e document root of &our Ce" server$ .. $=pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Herrorscript0$p p$

@ntrodução M *H*

*#gina 1 de +/

Exercício 5
="Bectivos: – <econ ecer tipos de coment#rios$ – )tilizar coment#rios$

Create a script full of comments so t at &ou can see oC t e&Lre ignored$ ;es, @Lm telling &ou to Crite a script t at does a"solutel& not ingR . $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-GCode CommentsDHT@TL-G DHH-A7G DQ=7;G DR66 T is is an HTML comment$ 66G / $ T&pe t e folloCing *H* code: DE HH T is is a simple *H* comment$ HU T is is a C6st&le, multiline comment$ ;ou can maAe t is as long as &ouLd liAe$ UH V )sed to s ellsE )se t is Aind of comment$ EG + $ Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG , $ %ave t e file Cit t e name comments$p p$ 1 $ *lace t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hcomments$p p$

@ntrodução M *H*

*#gina 2 de +/

Exercício 6
="Bectivos: – @dentificar vari#veis$ – 7istinguir tipos de dados armazenados numa vari#vel$ – 7istiguir tipos de vari#veis$ – Atri"uir valores a uma vari#vel$

Create a simple script t at assigns values to different varia"les and t en simpl& prints t e values to t e screen$ . $ =pen a neC file in &our text editor and t&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G*rinting 5aria"lesDHT@TL-G DHH-A7G DQ=7;G 0 $ Add a *H* "locA and create a varia"le t at olds an integer: DE Wint5ar J 8,,+0.,+1+I / $ Create a varia"le t at olds a floating6point num"er: Wfloat5ar J .,+0$00/00/,I + $ Create a varia"le t at olds a string: Wstring5ar J FT is is a string$FI , $ Add an ec o statement for eac varia"le: ec o FD*Ginteger: Wint5arDH*GFI ec o FD*Gfloat: Wfloat5arDH*GFI ec o FD*Gstring: Wstring5arDH*GFI 1 $ Close &our *H* "locA and add some more HTML so t at t e document is valid: EG DHQ=7;G DHHTMLG 2 $ %ave t e file Cit t e name printvarscript$p p and place t is file in t e document root of &our Ce" server$ 3 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hprintvarscript$p p$

Loca' and G'oba' (ariab'es
5aria"les can "e local or glo"al, t e difference aving to do Cit t eir definition and use "& t e programmer, and C ere t e& appear in t e context of t e scripts &ou are creating$ T e varia"les descri"ed in t e previous section, and for t e maBorit& of t is "ooA, are local varia"les$
@ntrodução M *H* *#gina 3 de +/

K en &ou Crite *H* scripts t at use varia"les, t ose varia"les can "e used onl& "& t e script in C ic t e& live$ %cripts cannot magicall& reac inside ot er scripts and use t e varia"les created and defined t ereSunless &ou sa& t e& can and &ou purposel& linA t em toget er$ K en &ou do Bust t at, suc as C en &ou create &our oCn functions '"locAs of reusa"le code t at perform a particular tasA(, &ou Cill define t e s ared varia"les as glo"al$ T at is, &ou Cill define t em as a"le to "e accessed "& ot er scripts and functions, as needed$ :or noC, Bust understand t at t ere are tCo varia"le scopesSlocal and glo"alSt at come into pla& as &ou Crite more advanced scripts$

$re)De*ined (ariab'es
@n all *H* scripts, a set of pre6defined varia"les is availa"le to &ou$ ;ou mig t ave seen some of t ese varia"les in t e output of t e p pinfo'( function, if &ou scrolled and read t roug t e entire results page$ %ome of t ese pre6defined varia"les are called superglo"als, meaning t at t e& are alCa&s present and availa"le to all of &our scripts, Cit out an& intervention "& &ou, t e programmer$ *lease stud& t e folloCing list of superglo"als, "ecause t e& Cill "e used exclusivel& t roug out t is "ooA$ -ac of t ese superglo"als is actuall& an arra& of ot er varia"les$ 7onLt Corr& a"out full& understanding t is concept noC, "ecause it Cill "e explained as &ou move t roug t e "ooA$ W>4-T contains an& varia"les provided to a script t roug t e 4-T met od$ W>*=%T contains an& varia"les provided to a script t roug t e *=%T met od$ W>C==X@- contains an& varia"les provided to a script t roug a cooAie$ W>:@L-% contains an& varia"les provided to a script t roug file uploads$ W>-?5 contains an& varia"les provided to a script as part of t e server environment$ W>%-%%@=? contains an& varia"les t at are registered in a session$

7istingue os trYs tipos de vari#veis: local, glo"al e superglo"al$ 7istingue as vari#veis superglo"ais W>4-T e W>*=%T recorrendo a um exemplo$

@ntrodução M *H*

*#gina 8 de +/

Exercício 7
="Bectivos: – @dentificar a função de uma constante$ – <econ ecer constantes$ – )tilizar constantes e definir o seu valor$

A constant is an identifier for a value t at cannot c ange during t e course of a script$ =nce a constant as a value, it remains t roug t e constantLs execution lifetime$ Constants can "e user6 defined, or &ou can use some of t e predefined constants t at *H* alCa&s as availa"le$ )nliAe simple varia"les, constants do not ave a dollar sign "efore t eir names, and t e& are usuall& uppercase to s oC t eir difference from scalar varia"les$ ?ext, &ouLll test t e user6defined t&pe$ . $ =pen a neC file in &our text editor and open a *H* "locA: DE 0 $ T e function used to define a constant is called define'(, and it reOuires t e name of t e constant and t e value &ou Cant to give it$ Here &ou define a constant called M;C=?%TA?T Cit a value of FT is is a test of defining constants$F$ define'FM;C=?%TA?TF, FT is is a test of defining constants$F(I / $ *rint t e value of t e constant, and t en close t e *H* "locA: ec o M;C=?%TA?TI + $ %ave t e file Cit t e name constants$p p and place t is file in t e document root of &our Ce" server$ , $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hconstants$p p$

@ntrodução M *H*

*#gina .9 de +/

Exercício 8
="Bectivos: – @dentificar constantes prZ6definidas$ – <econ ecer a função de constantes prZ6definidas$

%ome predefined constants include: >>:@L->> T e name of t e script file "eing parsed$ >>L@?->> T e num"er of t e line in t e script "eing parsed$ *H*>5-<%@=? T e version of *H* in use$ *H*>=% T e operating s&stem using *H* . $ =pen a neC file in &our text editor and open a *H* "locA: DE 0 $ )se t e ec o statement to displa& an introductor& string, and concatenate t e >>:@L->> constant to t e end of it: ec o FD"rGT is file is F$>>:@L->>I ?ote: Concatenate means to add one string to t e end of anot er, maAing a neC string$ / $ )se t e ec o statement to displa& an introductor& string, and concatenate t e >>L@?->> constant to t e end of it: ec o FD"rGT is is line num"er F$>>L@?->>I + $ )se t e ec o statement to displa& an introductor& string, and concatenate t e *H*>5-<%@=? constant to t e end of it: ec o FD"rG@ am using F$*H*>5-<%@=?I , $ )se t e ec o statement to displa& an introductor& string, and concatenate t e *H*>=% constant to t e end of it$ Also close up t e *H* "locA: ec o FD"rGT is test is "eing run on F$*H*>=%I EG 1 $ %ave t e file Cit t e name constants0$p p and place t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hconstants0$p p$

@ntrodução M *H*

*#gina .. de +/

Exercício 9
="Bectivos: – 7istinguir operadores de atri"uição entre si$ – <econ ecer exemplos e não exemplos de aplicação de operadores de atri"uição$ *rZ6reOuisitos: – Atri"uir valores a uma vari#vel$ Operator [J 6J $J Example [J Wa [J /I 6J Wa 6J /I $J Wa $J FstringFI Action C anges t e value of a varia"le to t e current value plus t e value on t e rig t side$ C anges t e value of a varia"le to t e current value minus t e value on t e rig t side Concatenates 'adds on to( t e value on t e rig t side Cit t e current value

. $ =pen a neC file in &our text editor and t&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G)sing Assignment =peratorsDHT@TL-G DHH-A7G DQ=7;G 0 $ %tart a *H* "locA$ Create a varia"le Cit a value of .99 and t en print it: DE Worig5ar J .99I ec o FD*G=riginal value is Worig5arDH*GFI / $ Add to t at value and t en print it: Worig5ar [J 0,I ec o FD*GAdded a value, noC itLs Worig5arDH*GFI + $ %u"tract from t at value and t en print it: Worig5ar 6J .0I ec o FD*G%u"tracted a value, noC itLs Worig5arDH*GFI , $ Concatenate a string and t en print it: Worig5ar $J F c icAensFI ec o FD*G:inal ansCer: Worig5arDH*GFI 1 $ Close &our *H* "locA and add some more HTML so t at t e document is valid: EG DHQ=7;G DHHTMLG 2 $ %ave t e file Cit t e name assignscript$p p and place t is file in t e document root of &our Ce" server$
@ntrodução M *H* *#gina .0 de +/

3 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hassignscript$p p$

@ntrodução M *H*

*#gina ./ de +/

Exercício 10
="Bectivos: – 7istinguir operadores aritmZticos entre si$ – <econ ecer exemplos e não exemplos de aplicação de operadores aritmZticos$ *rZ6reOuisitos: – Atri"uir valores a uma vari#vel$ – @dentificar função da T '"arra esOuerda( =perator [ 6 U H \ -xample W" J Wa [ /I W" J Wa 6 /I W" J Wa U /I W" J Wa H /I W" J Wa \ /I Action Adds values %u"tracts values Multiplies values 7ivides values <eturns t e modulus, or remainder

. $ =pen a neC file in &our text editor and t&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G)sing Arit metic =peratorsDHT@TL-G DHH-A7G DQ=7;G 0 $ %tart a *H* "locA, create tCo varia"les Cit values, and print t e values: DE Wa J 3,I W" J 0+I ec o FD*G=riginal value of TWa is Wa and TW" is W"DH*GFI ?ote: @f &ou escape t e dollar sign 'TW(, it Cill print literall& instead of "eing interpreted as a varia"le$ / $ Add t e tCo values and print t e result: Wc J Wa [ W"I ec o FD*GAdded TWa and TW" and got WcDH*GFI + $ %u"tract t e tCo values and print t e result: Wc J Wa 6 W"I ec o FD*G%u"tracted TW" from TWa and got WcDH*GFI
@ntrodução M *H* *#gina .+ de +/

, $ Multipl& t e tCo values and print t e result: Wc J Wa U W"I ec o FD*GMultiplied TWa and TW" and got WcDH*GFI 1 $ 7ivide t e tCo values and print t e result: Wc J Wa H W"I ec o FD*G7ivided TWa "& TW" and got WcDH*GFI 2 $ C ecA t e modulus of t e tCo values and print t e result: Wc J Wa \ W"I ec o FD*GT e modulus of TWa and TW" is WcDH*GFI 3 $ Close &our *H* "locA and add some more HTML so t at t e document is valid: EG DHQ=7;G DHHTMLG 8 $ %ave t e file Cit t e name arit meticscript$p p and place t is file in t e document root of &our Ce" server$ .9 $=pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Harit meticscript$p p$

@ntrodução M *H*

*#gina ., de +/

Exercício 11
="Bectivos: – 7istinguir operadores de comparação entre si$ – <econ ecer exemplos e não exemplos de aplicação de operadores de comparação$ *rZ6reOuisitos: – Atri"uir valores a uma vari#vel$ – @dentificar função da T '"arra esOuerda($ – )tilizar estruturas de decisão$ . $ =pen a neC file in &our text editor and t&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G)sing Comparison =peratorsDHT@TL-G DHH-A7G DQ=7;G 0 $ %tart a *H* "locA, create tCo varia"les Cit values, and print t e values: DE Wa J 0.I W" J .,I ec o FD*G=riginal value of TWa is Wa and TW" is W"DH*GFI / $ Kit in an if]else statement, test C et er Wa is eOual to W"$ 7epending on t e ansCer 'true or false(, one of t e ec o statements Cill print: if 'Wa JJ W"( ^ ec o FD*GT-%T .: TWa eOuals TW"DH*GFI _ else ^ ec o FD*GT-%T .: TWa is not eOual to TW"DH*GFI _ ?ote: Conditional expressions are enclosed in parent eses, suc as t is: if 'Wa JJ W"( and not t is: if Wa JJ W" + $ Kit in an if]else statement, test C et er Wa is not eOual to W"$ 7epending on t e ansCer 'true or false(, one of t e ec o statements Cill print: if 'Wa RJ W"( ^ ec o FD*GT-%T 0: TWa is not eOual to TW"DH*GFI _ else ^ ec o FD*GT-%T 0: TWa is eOual to TW"DH*GFI _ ?ote: T e curl& "races ^ and _ separate t e "locAs of statements Cit in a control structure$ , $ Kit in an if]else statement, test C et er Wa is greater t an W"$ 7epending on t e ansCer
@ntrodução M *H* *#gina .1 de +/

'true or false(, one of t e ec o statements Cill print: if 'Wa G W"( ^ ec o FD*GT-%T /: TWa is greater t an TW"DH*GFI _ else ^ ec o FD*GT-%T /: TWa is not greater t an TW"DH*GFI _ 1 $ Kit in an if]else statement, test C et er Wa is less t an W"$ 7epending on t e ansCer 'true or false(, one of t e ec o statements Cill print: if 'Wa D W"( ^ ec o FD*GT-%T +: TWa is less t an TW"DH*GFI _ else ^ ec o FD*GT-%T +: TWa is not less t an TW"DH*GFI _ 2 $ Kit in an if]else statement, test C et er Wa is greater t an or eOual to W"$ 7epending on t e ansCer 'true or false(, one of t e ec o statements Cill print: if 'Wa GJ W"( ^ ec o FD*GT-%T ,: TWa is greater t an or eOual to TW"DH*GFI _ else ^ ec o FD*GT-%T ,: TWa is not greater t an or eOual to TW"DH*GFI _ 3 $ Kit in an if]else statement, test C et er Wa is less t an or eOual to W"$ 7epending on t e ansCer 'true or false(, one of t e ec o statements Cill print: if 'Wa DJ W"( ^ ec o FD*GT-%T 1: TWa is less t an or eOual to TW"DH*GFI _ else ^ ec o FD*GT-%T 1: TWa is not less t an or eOual to TW"DH*GFI _ 8 $ Close &our *H* "locA and add some more HTML so t at t e document is valid: EG DHQ=7;G DHHTMLG .9 $%ave t e file Cit t e name comparisonscript$p p, and place t is file in t e document root of &our Ce" server$ .. $=pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hcomparisonscript$p p$

@ntrodução M *H*

*#gina .2 de +/

Exercício 12
="Bectivos: – 7istinguir operadores lNgicos entre si$ – <econ ecer exemplos e não exemplos de aplicação de operadores lNgicos$ *rZ6reOuisitos: – Atri"uir valores a uma vari#vel$ – )tilizar estruturas de decisão$ Logical operators alloC &our script to determine t e status of conditions 'suc as t e comparisons in t e preceding section($ @n t e context of if]else or C ile statements, logical operators execute certain code "ased on C ic conditions are true and C ic are false$ :or noC, focus on t e `` 'and( and aa 'or( operators to determine t e validit& of a feC comparisons$ . $ =pen a neC file in &our text editor and t&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G)sing Logical =peratorsDHT@TL-G DHH-A7G DQ=7;G 0 $ %tart a *H* "locA and create tCo varia"les Cit values$ T e comparisons in t is script are "ased on t ese tCo varia"les: DE Wdegrees J F8,FI W ot J F&esFI / $ Kit in an if]else statement, test C et er Wdegrees is greater t an .99 or if t e value of W ot is F&es$F 7epending on t e result of t e tCo comparisons, one of t e ec o statements Cill print: if ''Wdegrees G .99( aa 'W ot JJ F&esF(( ^ ec o FD*GT-%T .: @tLs DstrongGreall&DHstrongG otRDH*GFI _ else ^ ec o FD*GT-%T .: @tLs "eara"le$DH*GFI _ ?ote: Qecause t is conditional expression is actuall& made up of tCo smaller conditional expressions, an extra set of parent eses surrounds it$ + $ <epeat t e same if]else statement as in step /, "ut c ange t e operator from t e aa operator to t e `` operator: if ''Wdegrees G .99( `` 'W ot JJ F&esF(( ^ ec o FD*GT-%T 0: @tLs DstrongGreall&DHstrongG otRDH*GFI _ else ^ ec o FD*G T-%T 0: @tLs "eara"le$DH*GFI _ , $ Close &our *H* "locA and add some more HTML so t at t e document is valid:
@ntrodução M *H* *#gina .3 de +/

EG DHQ=7;G DHHTMLG 1 $ %ave t e file Cit t e name logicalscript$p p, and place t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hlogicalscript$p p$

@ntrodução M *H*

*#gina .8 de +/

Exercício 13
="Bectivos: – Aplicar vari#veis superglo"ais W>4-T e W>*=%T utilizando formul#rios$ *rZ6reOuisitos: – Compreender parbmetros da tag :=<M do HTML$ – Aplicar tags para inserção de texto e "ot!es$ – <esolver pro"lemas com a utilização de vari#veis$ – <ealizar tarefas Oue envolvem a aplicação de operadores$ – <esolver pro"lemas utilizando estruturas de decisão$ @n &our HTML code, t e first line of a form looAs somet ing liAe t is: D:=<M M-TH=7JFpostF ACT@=?JF&ourscript$p pFG K en &ou clicA on a su"mit "utton in an HTML form, varia"les are sent to t e script specified "& t e ACT@=? via t e specified M-TH=7$ T e met od can "e eit er *=%T or 4-T$ 5aria"les passed from a form to a *H* script are placed in t e superglo"al called W>*=%T or W>4-T, depending on t e form met od$ @n t e next section, &ou see oC t is CorAs, "& creating an HTML form and accompan&ing *H* script t at performs calculations depending on t e form input$

Creatin+ a Ca'c 'ation ,or@n t is section, &ouLll create t e front end to a calculation script$ T is form Cill contain tCo input fields and a radio "utton to select t e calculation t&pe$ . $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-GCalculation :ormDHT@TL-G DHH-A7G DQ=7;G / $ Qegin &our form$ Assume t at t e met od is *=%T and t e action is a script called calculate$p p: D:=<M M-TH=7JFpostF ACT@=?JFcalculate$p pFG + $ Create an input field for t e first value, Cit a text la"el: D*G5alue .: D@?*)T T;*-JFtextF ?AM-JFval.F %@c-J.9GDH*G , $ Create an input field for t e second value, Cit a text la"el: D*G5alue 0: D@?*)T T;*-JFtextF ?AM-JFval0F %@c-J.9GDH*G 1 $ Add a su"mit "utton: D*GD@?*)T T;*-JFsu"mitF ?AM-JFsu"mitF 5AL)-JFCalculateFGDH*G 2 $ Close &our form and add more HTML so t at t e document is valid: DH:=<MG
@ntrodução M *H* *#gina 09 de +/

DHQ=7;G DHHTMLG 3 $ %ave t e file Cit t e name calculate>form$ tml, and place t is file in t e document root of &our Ce" server$ 8 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hcalculate>form$ tml$

T ereLs one more item to add: a series of radio "uttons to determine t e t&pe of calculation to perform on t e tCo values$ . $ =pen calculate>form$ tml in &our text editor$ 0 $ Add t is "locA "efore t e su"mit "utton: D*GCalculation:D"rG D@?*)T T;*-JFradioF ?AM-JFcalcF 5AL)-JFaddFG addD"rG D@?*)T T;*-JFradioF ?AM-JFcalcF 5AL)-JFsu"tractFG su"tractD"rG D@?*)T T;*-JFradioF ?AM-JFcalcF 5AL)-JFmultipl&FG multipl&D"rG D@?*)T T;*-JFradioF ?AM-JFcalcF 5AL)-JFdivideFG divideDH*G / $ %ave t e file and place it in t e document root of &our Ce" server$ + $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hcalculate>form$ tml$

Creatin+ t.e Ca'c 'ation Scri/t
According to t e form action in calculate>form$ tml, &ou need a script called calculate$p p$ T e goal of t is script is to accept t e tCo values 'W>*=%Tdval.e and W>*=%Tdval0e( and perform a calculation depending on t e value of W>*=%Tdcalce$ . $ =pen a neC file in &our text editor$ 0 $ %tart a *H* "locA and prepare an if statement t at c ecAs for t e presence of t e t ree values: DE if ''W>*=%Tdval.e JJ FF( aa 'W>*=%Tdval0e JJ FF( aa 'W>*=%Tdcalce JJFF(( ^ HH more code goes ere _ T is statement sa&s, F@f an& of t ese t ree varia"les do not ave a value, do somet ing else$F / $ <eplace t e Fmore code goes ereF Cit t e folloCing tCo lines: eader'FLocation: calculate>form$ tmlF(I exitI T e first of t ese tCo lines outputs a eader statementSin t is case, one t at sends t e "roCser "acA to t e calculation form$ T e second line causes t e script to exit$ %o, if an& of t e t ree reOuired varia"les do not ave a value, t is action Cill occur$ ?ote: Qe sure t at t ere are no line "reaAs, spaces, or an& ot er text "efore &our *H* "locA starts$ ;ou cannot use t e eader'( function if output as alread& "een sent to t e "roCser$ + $ Qegin an if]else statement to perform t e correct calculation, "ased on t e value of W>*=%Tdcalce, starting Cit a value of FaddF: if 'W>*=%Tdcalce JJ FaddF( ^
@ntrodução M *H* *#gina 0. de +/

Wresult J W>*=%Tdval.e [ W>*=%Tdval0eI , $ Continue t e statement for t e remaining t ree calculation t&pes, and t en close &our *H* "locA: _ else if 'W>*=%Tdcalce JJ Fsu"tractF( ^ Wresult J W>*=%Tdval.e 6 W>*=%Tdval0eI _ else if 'W>*=%Tdcalce JJ Fmultipl&F( ^ Wresult J W>*=%Tdval.e U W>*=%Tdval0eI _ else if 'W>*=%Tdcalce JJ FdivideF( ^ Wresult J W>*=%Tdval.e H W>*=%Tdval0eI _ EG 1 $ %tart t e HTML output: DHTMLG DH-A7G DT@TL-GCalculation <esultDHT@TL-G DHH-A7G DQ=7;G 2 $ )sing HTML mingled Cit *H* code, displa& t e value of Wresult: D*GT e result of t e calculation is: DE ec o FWresultFI EGDH*G 3 $ Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG 8 $ %ave t e file Cit t e name calculate$p p, and place t is file in t e document root of &our Ce" server$

S b-ittin+ 0o r ,or- and Gettin+ 1es 'ts
?oC t at &ouLve created "ot t e front end 'form( and t e "acA end 'script(, itLs time to old &our "reat and test it$ . $ To access t e calculation form, open &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hcalculate>form$ tml$ 0 $ ClicA on t e Calculate "utton Cit out t&ping an&t ing in t e form fields$ ;our Ce" "roCser Cill reload t e page "ecause &ou didnLt enter an& values for t e t ree reOuired fields$ / $ -nter a value for 5alue ., "ut not for 5alue 0, and do not select a calculation option$ After &ou clicA on Calculate, t e page s ould reload$ + $ -nter a value for 5alue 0, "ut not for 5alue ., and do not select a calculation option$ After &ou clicA on Calculate, t e page s ould reload$ , $ -nter a value for 5alue . and for 5alue 0, "ut do not select a calculation option$ After &ou clicA on Calculate, t e page s ould reload$ 1 $ %elect a calculation option, "ut do not enter an& values for 5alue . or 5alue 0$ After &ou clicA on Calculate, t e page s ould reload$ ?oC t at &ouLve de"ugged t e script "& attempting to "&pass &our validation routine, tr& some calculations, suc as t e folloCing:
@ntrodução M *H* *#gina 00 de +/

2 $ -nter 82/0 for 5alue . and 02 for 5alue 0$ 3 $ %elect add, and clicA on t e Calculate "utton$

@ntrodução M *H*

*#gina 0/ de +/

Exercício 14
="Bectivos: – @dentificar função das vari#veis glo"ais$ – 7istinguir a função das vari#veis glo"ais <-M=T->A77< e HTT*>)%-<>A4-?T$ – <ecorrer a funç!es e vari#veis glo"ais para o"ter informação$ – )sar a função getenv'($ *rZ6reOuisitos: – <esolver pro"lemas atravZs da aplicação de vari#veis$ K en a Ce" "roCser maAes a reOuest of a Ce" server, it sends along Cit t e reOuest a list of extra varia"les$ T ese are called environment varia"les, and t e& can "e ver& useful for displa&ing d&namic content or aut orizing users$ . $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing line of *H* code: DE p pinfo'(I EG / $ %ave t e file Cit t e name p pinfo$p p, and place t is file in t e document root of &our Ce" server$ + $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hp pinfo$p p$

1etrievin+ and sin+ 1E2OTE3ADD1
Q& default, environment varia"les are availa"le to *H* scripts as W5A<>?AM-$ :or example, t e <-M=T->A77< environment varia"le is alread& contained as W<-M=T->A77<$ HoCever, to "e a"solutel& sure t at &ouLre reading t e correct value, &ou can use t e getenv'( function to assign a value to a varia"le of &our c oice$ T e <-M=T->A77< environment varia"le contains t e @* address of t e mac ine maAing t e reOuest$ LetLs get t e value of &our <-M=T->A77<$ . $ =pen a neC file in &our text editor$ 0 $ =pen a *H* "locA, and t en use getenv'( to place t e value of <-M=T->A77< in a varia"le called Waddress: DE Waddress J getenv'F<-M=T->A77<F(I / $ *rint t e value of Waddress to t e screen, and close &our *H* "locA: ec o F;our @* address is Waddress$FI EG + $ %ave t e file Cit t e name remoteaddress$p p, and t en place t is file in t e document root of &our Ce" server$ , $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hremoteaddress$p p$

@ntrodução M *H*

*#gina 0+ de +/

1etrievin+ and sin+ %TT$34SE13AGENT
T e HTT*>)%-<>A4-?T varia"le contains t e "roCser t&pe, "roCser version, language encoding, and platform$ :or example, t e folloCing value string refers to t e ?etscape "roCser, version 2$., in -nglis , on t e KindoCs platform: MozillaH,$9 'KindoCsI )I KindoCs ?T ,$.I en6)%I rv:.$+( 4ecAoH099/910+ ?etscapeH2$. 'ax($ Here is anot er common HTT*>)%-<>A4-?T value: MozillaH+$9 'compati"leI M%@- 1$9I KindoCs ?T ,$.( T is value refers to Microsoft @nternet -xplorer 'M%@-( version 1$9 on KindoCs$ %ometimes &ou Cill see M%@- return an HTT*>)%-<>A4-?T value t at looAs liAe a ?etscape value, suc as t is one, C ic "egins Cit Mozilla, until &ou notice t at t e value sa&s itLs compati"le and is actuall& M%@- 1$9$ :inall&, donLt count out t e text6onl& "roCsersR A L&nx HTT*>)%-<>A4-?T value looAs somet ing liAe t is: L&nxH0$3rel$/ li"CCC6:MH0$.+ LetLs find &our HTT*>)%-<>A4-?T$ . $ =pen a neC file in &our text editor$=pen a *H* "locA, and t en use getenv'( to place t e value of HTT*>)%-<>A4-?T in a varia"le called Wagent: DE Wagent J getenv'FHTT*>)%-<>A4-?TF(I 0 $ *rint t e value of Wagent to t e screen, and t en close &our *H* "locA: ec o F ;ou are using Wagent$FI EG / $ %ave t e file Cit t e name useragent$p p, and t en place t is file in t e document root of &our Ce" server$ + $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Huseragent$p p$

@ntrodução M *H*

*#gina 0, de +/

Exercício 15
="Bectivos: – )sar a função preg>matc '($ *rZ6reOuisitos: – 7istinguir a função das vari#veis glo"ais <-M=T->A77< e HTT*>)%-<>A4-?T$ – <ecorrer a funç!es e vari#veis glo"ais para o"ter informação$ – )sar a função getenv'($ – <esolver pro"lemas atravZs da aplicação de vari#veis$ – <esolver pro"lemas utilizando estruturas de decisão$ Having seen some of t e possi"le values of HTT*>)%-<>A4-?T in t e last c apter, &ou can imagine t at t ere are undreds of slig tl& different values$ %o itLs time to learn some "asic pattern matc ing$ ;ouLll use t e preg>matc '( function to perform t is tasA$ T is function needs tCo arguments: C at &ouLre looAing for, and C ere &ouLre looAing: preg>matc 'FHdC at &ouLre looAing foreHF, FdC ere &ouLre looAingeF(I T is function returns a value of true or false, C ic &ou can use in an if]else "locA to do C atever &ou Cant$ T e goal of t e first script is to determine C et er a Ce" "roCser is Microsoft @nternet -xplorer, ?etscape, or somet ing else$ Kit in t e value of HTT*>)%-<>A4-?T, ?etscape alCa&s uses t e string Mozilla to identif& itself$ )nfortunatel&, t e value of HTT*>)%-<>A4-?T for Microsoft @nternet -xplorer also uses Mozilla to s oC t at itLs compati"le$ LucAil&, it also uses t e string M%@-, so &ou can searc for t at$ @f t e value of HTT*>)%-<>A4-?T doesnLt contain eit er Mozilla or M%@-, c ances are ver& good t at itLs not one of t ose Ce" "roCsers$ . $ =pen a neC file in &our text editor and start a *H* "locA, and t en use getenv'( to place t e value of HTT*>)%-<>A4-?T in a varia"le called Wagent: DE Wagent J getenv'FHTT*>)%-<>A4-?TF(I 0 $ %tart an if]else statement to find C ic of t e preg>matc '( functions is true, starting Cit t e searc for M%@-: if 'preg>matc 'FHM%@-HiF, FWagentF(( ^ Wresult J F;ou are using Microsoft @nternet -xplorer$FI _ / $ Continue t e statement, testing for FMozillaF: + $ :inis t e statement "& defining a default: else ^ Wresult J F;ou are using WagentFI _ , $ Close &our *H* "locA and add some HTML to "egin t e displa&: EG DHTMLG
@ntrodução M *H* *#gina 01 de +/

DH-A7G DT@TL-GQroCser Matc <esultsDHT@TL-G DHH-A7G DQ=7;G 1 $ T&pe t e folloCing *H* code to print t e result of t e if]else statement: DE ec o FD*GWresultDH*GFI EG 2 $ Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG 3 $ %ave t e file Cit t e name "roCsermatc $p p, and place t is file in t e document root of &our Ce" server$ 8 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.H"roCsermatc $p p$

@ntrodução M *H*

*#gina 02 de +/

Exercício 16
="Bectivos: – @ntegrar cNdigo HTML com *H* e C%%$ *rZ6reOuisitos: – )sar a função preg>matc '( e getenv'($ – -specificar parbmetros do resultado da vari#vel glo"al HTT*>)%-<>A4-?T$ – Aplicar Cascading %t&le % eets 'C%%( para formatar elementos de p#ginas mais comuns$ – <esolver pro"lemas utilizando estruturas de decisão$ T ere are differences not onl& "etCeen "roCsers, "ut also "etCeen platforms$ T is difference is most clear Cit regard to fonts and font sizes$ @n t e KindoCs Corld, &ou ave fonts suc as Times ?eC <oman and Courier ?eC$ %lig t variations of t ese fonts appear on t e Macintos and LinuxH)?@f platformsI t e& are called Times and Courier$ @t doesnLt end t ereSt e font sizes all are displa&ed differentl&$ A .96point font on Macintos or Linux is sometimes "arel& legi"le, "ut if &ou "ump it up to .. or .0 point, &ouLre in "usiness$ @f t at same .06point font is vieCed on KindoCs, oCever, it mig t looA liAe &our text is tr&ing to taAe over t e Corld$ %o C at to doE )se &our neC pattern6matc ing sAills to extract t e platform from t e HTT*>)%-<>A4-?T string, and t en displa& platform6specific HTML$ As Cit matc ing on a Ae&Cord to nail doCn t e platformSC ic &ou did in t e previous sectionS&ou also need to AnoC C at &ouLre looAing for$ @n t e next script, &ouLll c ecA for t e Ae&Cords FKinF and FLinuxF and print an appropriate st&le s eet "locA in &our HTML result page$ . $ =pen a neC file in &our text editor, start a *H* "locA, and use getenv'( to place t e value of HTT*>)%-<>A4-?T in a varia"le called Wagent: DE Wagent J getenv'FHTT*>)%-<>A4-?TF(I 0 $ %tart an if]else statement to find C ic of t e preg>matc '( functions is true, starting Cit t e searc for FKinF: if 'preg>matc 'FHKinHiF, FWagentF(( ^ Wst&le J FCinFI _ / $ Continue t e statement, testing for FLinuxF: else if 'preg>matc 'FHLinuxHiF, FWagentF(( ^ Wst&le J FlinuxFI _ + $ Create a "asic st&le s eet "locA for KindoCs users: WCin>st&le J FDst&le t&peJTFtextHcssTFGp, ul, ol, li ^font6famil&:ArialIfont6size:.9ptIfont6Ceig t:normalI_ . ^font6famil&:ArialIfont6size:.1ptIfont6Ceig t:"oldI_ 0 ^font6famil&:ArialIfont6size:.+ptIfont6Ceig t:"oldI_ strong ^font6famil&:ArialIfont6size:.9ptIfont6Ceig t:"oldI_ em ^font6famil&:ArialIfont6size:.9ptIfont6st&le:italicI_ DHst&leGFI
@ntrodução M *H* *#gina 03 de +/

?ote: K en &ou use Ouotation marAs inside ot er Ouotation marAs, t e inner pair must "e delineated from t e outside pair using t e escape 'T( c aracter 'also AnoCn as a "acAslas ($ , $ Create a "asic st&le s eet "locA for Linux users: Wlinux>st&le J FDst&le t&peJTFtextHcssTFG p, ul, ol, li ^font6famil&:TimesIfont6size:.0ptIfont6Ceig t:normalI_ . ^font6famil&:TimesIfont6size:.3ptIfont6Ceig t:"oldI_ 0 ^font6famil&:TimesIfont6size:.1ptIfont6Ceig t:"oldI_ strong ^font6famil&:TimesIfont6size:.0ptIfont6Ceig t:"oldI_ em ^font6famil&:TimesIfont6size:.0ptIfont6st&le:italicI_ DHst&leGFI 1 $ Close &our *H* "locA and add t e folloCing HTML: EG DHTMLG DH-A7G DT@TL-G*latform Matc ingDHT@TL-G 2 $ T&pe t e folloCing *H* code, creating an if]else statement used to print t e correct st&le s eet "locA: DE if 'Wst&le JJ FCinF( ^ ec o FWCin>st&leFI _ else if 'Wst&le JJ FlinuxF( ^ ec o FWlinux>st&leFI _ EG 3 $ Close t e top section of &our HTML and start t e "od&: DHH-A7G DQ=7;G 8 $ T&pe t e folloCing HTML to s oC t e use of &our st&le s eet: D . alignJcenterGT is is a level . eadingDH .G D 0 alignJcenterGLooAR A level 0 eadingDH 0G D* alignJcenterGT is is a simple paragrap Cit some DstrongG"oldDHstrongG and DemGemp asizedDHemG text$DH*G .9 $Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG .. $%ave t e file Cit t e name platformmatc $p p, and place it in t e document root of &our Ce" server$ .0 $=pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hplatformmatc $p p$

@ntrodução M *H*

*#gina 08 de +/

Exercício 17
="Bectivos: – <econ ecer utilidade das funç!es com strings$ – @dentificar finalidade de cada função com strings$ – <esolver peOuenos pro"lemas aplicando funç!es com strings$ *rZ6reOuisitos: – Aplicar tags para inserção de texto, formul#rios e "ot!es$ – Aplicar vari#veis superglo"ais W>4-T e W>*=%T utilizando formul#rios$ – <esolver pro"lemas atravZs da aplicação de vari#veis$ – <ealizar tarefas Oue envolvem a aplicação de operadores$ – <esolver pro"lemas utilizando estruturas de decisão$ ?umerous string functions are "uilt into *H*, all of C ic are designed to maAe &our life easier$ %uppose &ou ave to normalize strings for neCs eadlines or product @7 num"ers, or calculate t e lengt of a string "efore tr&ing to stuff it into a data"ase field$ T ose are Bust a feC of t e string functions &ouLll learn a"out in t e next section$ :or more string functions, taAe a looA at t e *H* manual at ttp:HHCCC$p p$netHstrings$ T e function list groCs dail& as more people contri"ute to t e language$

Creatin+ an In/ t ,or@n t is section, &ouLll create t e front end to a string modification script$ T is form Cill contain one text area and several radio "uttons$ T e radio "uttons Cill determine t e string function to use$ . $ =pen a neC file in &our text editor and t&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G4eneric @nput :ormDHT@TL-G DHH-A7G DQ=7;G 0 $ Qegin &our form$ Assume t at t e met od is *=%T and t e action is a script called displa&>input$p p: D:=<M M-TH=7JFpostF ACT@=?JFdispla&>input$p pFG / $ Create a text area Cit a text la"el: D*GDstrongGText :ield:DHstrongGD"rG DT-fTA<-A ?AM-JFtext.F C=L%J+, <=K%J, K<A*JvirtualGDHT-fTA<-AGDH*G + $ Add t is "locA of radio "uttons: D*GDstrongG%tring :unction:DHstrongGD"rG D@?*)T T;*-JFradioF ?AM-JFfuncF 5AL)-JFmd,FG get md,D"rG D@?*)T T;*-JFradioF ?AM-JFfuncF 5AL)-JFstrlenFG get lengt of stringD"rG D@?*)T T;*-JFradioF ?AM-JFfuncF 5AL)-JFstrrevFG reverse t e stringD"rG
@ntrodução M *H* *#gina /9 de +/

D@?*)T T;*-JFradioF ?AM-JFfuncF 5AL)-JFstrtoupperFG maAe string uppercaseD"rG D@?*)T T;*-JFradioF ?AM-JFfuncF 5AL)-JFstrtoloCerFG maAe string loCercaseD"rG D@?*)T T;*-JFradioF ?AM-JFfuncF 5AL)-JFucCordsFG maAe first letter of all Cords uppercaseDH*G ?ote: T e value for eac radio "utton is its exact *H* function name$ T is Cill maAe t e "acA6end script ver& simple to create, as &ouLll see in t e next section$ , $ Add a su"mit "utton: D*GD@?*)T T;*-JFsu"mitF ?AM-JFsu"mitF 5AL)-JF7o %omet ing Kit t e %tringFGDH*G 1 $ Close &our form and add some more HTML so t at t e document is valid: DH:=<MG DHQ=7;G DHHTMLG 2 $ %ave t e file Cit t e name generic>form$ tml, and place t is file in t e document root of &our Ce" server$ 3 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hgeneric>form$ tml$

Creatin+ a Scri/t to Dis/'a5 ,or- (a' es
According to t e form action in generic>form$ tml, &ou need a script called displa&>input$p p$ T e goal of t is script is to accept t e text in W>*=%Tdtext.e and use a particular string function 't e value of W>*=%Tdfunce( to get a neC result 'Wresult($ . $ =pen a neC file in &our text editor and t&pe t e folloCing *H* code$ T is Cill ensure t at t e user is sent "acA to t e form if no value is entered in t e text area and no function is selected from t e list of radio "uttons: DE if ''W>*=%Tdtext.e JJ FF( aa 'W>*=%Tdfunce JJ FF(( ^ eader'FLocation: generic>form$ tmlF(I exitI _ 0 $ T&pe t e next "it of *H*, C ic assigns t e value of t e function output to a varia"le called Wresult, and t en close t e *H* "locA: Wresult J W>*=%Tdfunce'W>*=%Tdtext.e(I EG / $ %tart t e HTML output: DHTMLG DH-A7G DT@TL-G4eneric @nput <esultsDHT@TL-G DHH-A7G DQ=7;G + $ 7ispla& t e value of Wresult: DE ec o FWresultFI EG
@ntrodução M *H* *#gina /. de +/

, $ Add a linA "acA to t e form: D*GDa refJFgeneric>form$ tmlFG4o againRDHaGDH*G 1 $ Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG 2 $ %ave t e file Cit t e name displa&>input$p p, and place t is file in t e document root of &our Ce" server$;our code s ould looA somet ing liAe t is: DE if ''W>*=%Tdfunce JJ FF( aa 'W>*=%Tdtext.e JJ FF(( ^ eader'FLocation: generic>form$ tmlF(I exitI _ Wresult J W>*=%Tdfunce'W>*=%Tdtext.e(I EG DHTMLG DH-A7G DT@TL-G4eneric @nput <esultsDHT@TL-G DHH-A7G DQ=7;G DE ec o FWresultFI EG DpGDa refJFgeneric>form$ tmlFG4o againRDHaGDHpG DHQ=7;G DHHTMLG

S b-ittin+ 0o r ,or- and Gettin+ 1es 'ts
?oC t at &ouLve created "ot a front6end form and a "acA6end script, itLs time to tr& t em out$ . $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hgeneric>form$ tml$ 0 $ T&pe t e folloCing text in t e text area: @ t inA *H* is Bust t e coolest server6side scripting language aroundR K o AneC it Could "e t is simpleE ?ote: ;ou mig t Cant to cop& t at c unA of text to t e clip"oard, "ecause it Cill "e used in all of t e folloCing examples$ / $ %elect t e get md, radio "utton, and clicA on t e 7o %omet ing Kit t e %tring "utton$

@ntrodução M *H*

*#gina /0 de +/

Exercício 18
="Bectivos: – *rZ6reOuisitos: – <edirecting a user to a neC location means t at &our script as sent an HTT* eader to t e "roCser, indicating a neC location$ HTT* eaders of an& Aind 'aut entication, redirection, cooAies, and so on( must "e sent to t e "roCser "efore an&t ing else, including C ite space, line "reaAs, and an& c aracters$ Alt oug &ouLve alread& used t e eader'( function to redirect t e user in t e case of an incomplete form, in t e next section &ou create a specific redirection menu$ T e goal is to ave t e users select a neC location from a drop6doCn menu and t en ave t e script automaticall& send t em t ere$

Creatin+ a 1edirection ,or@n t is section, &ouLll create t e front end to a redirection script$ T is form Cill contain a drop6doCn list of t e names of various Ce"sites$ T e value for eac option is t e Ce"siteLs )<L$ . $ =pen a neC file in &our text editor and t&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G <edirection MenuDHT@TL-G DHH-A7G DQ=7;G 0 $ Qegin &our form$ Assume t at t e met od is *=%T and t e action is a script called do>redirect$p p: D:=<M M-TH=7JFpostF ACT@=?JFdo>redirect$p pFG / $ Add t is drop6doCn list: D*G%end me to: D%-L-CT nameJFlocationFG D=*T@=? valueJFFG66 %elect =ne 66DH=*T@=?G D=*T@=? valueJF ttp:HHCCC$t icA"ooA$comHFGt icA"ooA$comDH=*T@=?G D=*T@=? valueJF ttp:HHCCC$i0ii$comHFGi0i @nteractiveDH=*T@=?G D=*T@=? valueJF ttp:HHCCC$p p$netHFG*H*$netDH=*T@=?G D=*T@=? valueJF ttp:HHCCC$zend$comHFGcend Tec nologiesDH=*T@=?G DH%-L-CTG + $ Add a su"mit "utton: D*GD@?*)T T;*-JFsu"mitF ?AM-JFsu"mitF 5AL)-JF4oRFGDH*G , $ Close &our form, and add some more HTML so t at t e document is valid: DH:=<MG

@ntrodução M *H*

*#gina // de +/

DHQ=7;G DHHTMLG 1 $ %ave t e file Cit t e name redirect>form$ tml, and place t is file in t e document root of &our Ce" server$ 2 $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hredirect>form$ tml$

Creatin+ t.e 1edirection Scri/t and Testin+ It
According to t e form action in redirect>form$ tml, &ou need a script called do>redirect$p p$ T e goal of t is script is to accept t e value of W>*=%Tdlocatione and print t at value Cit in t e eader'( function so t at t e user is redirected to t e c osen location$ . $ =pen a neC file in &our text editor and t&pe t e folloCing *H* to create t e proper redirection eader, including a c ecA to ensure t at somet ing Cas selected: DE if 'W>*=%Tdlocatione JJ FF(^ eader'FLocation: redirect>form$ tmlF(I exitI _ else ^ eader'FLocation: W>*=%TdlocationeF(I exitI _ EG 0 $ %ave t e file Cit t e name do>redirect$p p, and place t is file in t e document root of &our Ce" server$ / $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hredirect>form$ tml$ + $ %elect *H*$net from t e drop6doCn list, and clicA on t e 4oR "utton$

@ntrodução M *H*

*#gina /+ de +/

Exercício 19
="Bectivos: – @ntegrar cNdigo HTML com cNdigo *H*$ *rZ6reOuisitos: – Aplicar tags para inserção de texto, formul#rios e "ot!es$ – <esolver pro"lemas atravZs da aplicação de vari#veis$ – <ealizar tarefas Oue envolvem a aplicação de operadores$ – <esolver pro"lemas utilizando estruturas de decisão$ – <ecorrer a funç!es e vari#veis glo"ais para o"ter informação$ A simple feed"acA form usuall& contains fields for t e respondentLs name and e6mail address, and a text area for some sort of message$ @n t is section, &ou create tCo files: one for t e feed"acA form, and one for t e *H* script to process t e form, send t e mail, and return a response to t e "roCser$

Creatin+ t.e ,eedbac6 ,or@n t is section, &ou create t e first alf of t e formHscript com"oSt e feed"acA form itself, often referred to as t e front6end form$ . $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-G%imple :eed"acA :ormDHT@TL-G DHH-A7G DQ=7;G / $ Qegin &our form$ Assume t at t e met od is *=%T and t e action is a script called send>simpleform$p p: D:=<M M-TH=7JF*=%TF ACT@=?JFsend>simpleform$p pFG + $ Create an input field for t e userLs name Cit a text la"el: D*GDstrongG;our ?ame:DHstrongGD"rG D@?*)T t&peJFtextF ?AM-JFsender>nameF %@c-J/9GDH*G , $ Create an input field for t e userLs e6mail address Cit a text la"el: D*GDstrongG;our -6Mail Address:DHstrongGD"rG D@?*)T t&peJFtextF ?AM-JFsender>emailF %@c-J/9GDH*G 1 $ Create a text area to old t e message Cit a text la"el: D*GDstrongGMessage:DHstrongGD"rG DT-fTA<-A ?AM-JFmessageF C=L%J/9 <=K%J, K<A*JvirtualGDHT-fTA<-AGDH*G 2 $ Add a su"mit "utton: D*GD@?*)T T;*-JFsu"mitF ?AM-JFsu"mitF 5AL)-JF%end T is :ormFGDH*G 3 $ Close &our form and add some more HTML so t at t e document is valid:
@ntrodução M *H* *#gina /, de +/

DH:=<MG DHQ=7;G DHHTMLG 8 $ %ave t e file Cit t e name simple>form$ tml, and place t is file in t e document root of &our Ce" server$ .9 $=pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hsimple>form$ tml$

Creatin+ a Scri/t to 2ai' 0o r ,orAccording to t e form action in simple>form$ tml, &ou need a script called send>simpleform$p p$ T e goal of t is script is to accept t e text in W>*=%Tdsender>namee, W>*=%Tdsender>emaile, and W>*=%Tdmessagee format, send an e6mail, and displa& a confirmation to t e Ce" "roCser$ . $ =pen a neC file in &our text editor$ 0 $ Qegin a *H* "locA, and t en add some error6c ecAing code into t e mix: DE if ''W>*=%Tdsender>namee JJ FF( aa 'W>*=%Tdsender>emaile JJ FF( aa 'W>*=%Tdmessagee JJ FF(( ^ eader'FLocation: simple>form$ tmlF(I exitI _ / $ %tart "uilding a message string, C ic Cill contain t e content for t e e6mail itself: Wmsg J F-6MA@L %-?T :<=M KKK %@T-TnFI + $ Continue "uilding t e message string "& adding an entr& for t e senderLs name: ?ote T e next feC steps Cill continue "uilding t e message string "& concatenating smaller strings to form one long message string$ , $ Continue "uilding t e message string "& adding an entr& for t e senderLs e6mail address: Wmsg $J F%enderLs -6Mail:TtW>*=%Tdsender>emaileTnFI 1 $ Continue "uilding t e message string "& adding an entr& for t e message: Wmsg $J FMessage:TtW>*=%TdmessageeTnFI 2 $ Create a varia"le to old t e recipientLs e6mail address 'su"stitute &our oCn(: Wto J F&oug&ouremail$comFI 3 $ Create a varia"le to old t e su"Bect of t e e6mail: Wsu"Bect J FKe" %ite :eed"acAFI 8 $ Create a varia"le to old additional mail eaders: Wmail eaders J F:rom: M& Ke" %ite Dgenericaddressg&ourdomain$comGTnFI ?ote: Mail eaders are t e strings at t e "eginning of mail messages t at formulate t eir structure and essentiall& maAe t em valid mail messages$ .9 $Add t e folloCing to t e Wmail eaders varia"le: Wmail eaders $J F<epl&6To: W>*=%Tdsender>emaileTnFI
@ntrodução M *H* *#gina /1 de +/

.. $Add t e mail'( function: mail'Wto, Wsu"Bect, Wmsg, Wmail eaders(I .0 $Close &our *H* "locA: EG Alt oug t is code Cill send t e mail, &ou s ould return somet ing to t e userLs screen so t at e AnoCs t e form as "een sent$ =t erCise, e mig t sit t ere and continuall& clicA t e %end T is :orm "utton$ ./ $%tart t e HTML output: DHTMLG DH-A7G DT@TL-G%imple :eed"acA :orm %entDHT@TL-G DHH-A7G DQ=7;G .+ $Add some information to tell t e user C at as appened: DH.GT e folloCing e6mail as "een sent:DHH.G ., $Add t e text la"el for t e ;our ?ame field and displa& t e userLs input, and do t e same for t e ot er fields: D*GDstrongG;our ?ame:DHstrongGD"rG DE ec o FW>*=%Tdsender>nameeFI EG D*GDstrongG;our -6Mail Address:DHstrongGD"rG DE ec o FW>*=%Tdsender>emaileFI EG D*GDstrongGMessage:DHstrongGD"rG DE ec o FW>*=%TdmessageeFI EG .1 $Add some more HTML so t at t e document is valid: DHQ=7;G DHHTMLG .2 $%ave t e file Cit t e name send>simpleform$p p, and place t is file in t e document root of &our Ce" server$

S b-ittin+ 0o r ,or- and Gettin+ 1es 'ts
?oC t at &ouLve created "ot a front6end form and a "acA6end script, itLs time to tr& t em out$ . $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hsimple>form$ tml$ 0 $ T&pe &our name in t e ;our ?ame field$T&pe &our e6mail address in t e ;our -6Mail Address field$ / $ T&pe t e folloCing message in t e Message field: *H* is so coolR + $ ClicA on t e %end T is :orm "utton$ T e information &ou entered, along Cit a confirmation t at &our e6mail as "een sent, Cill appear$ ?oC c ecA &our e6mail, and see if a message is Caiting for &ou$

@ntrodução M *H*

*#gina /2 de +/

. $ =pen send>simpleform$p p in &our text editor$ 0 $ Modif& t e string containing %enderLs ?ame "& replacing t e ta" c aracter 'Tt( Cit tCo spaces: Wmsg $J F%enderLs ?ame: W>*=%Tdsender>name_TnFI / $ Modif& t e string containing %enderLs -6Mail "& replacing t e ta" c aracter 'Tt( Cit four spaces: Wmsg $J F%enderLs -6Mail: W>*=%Tdsender>emaileTnFI + $ Modif& t e string containing Message "& replacing t e ta" c aracter 'Tt( Cit .9 spaces: Wmsg $J FMessage: W>*=%TdmessageeTnFI , $ %ave t e file and upload it to &our server$

@ntrodução M *H*

*#gina /3 de +/

Exercício 20
="Bectivos: – )sar a vari#vel glo"al W>%-<5-<d*H*>%-L:e$ – @dentificar conceitos e mZtodos necess#rios para resolver pro"lemas$ *rZ6reOuisitos: – Aplicar tags para inserção de texto, formul#rios e "ot!es$ – <esolver pro"lemas atravZs da aplicação de vari#veis$ – <ealizar tarefas Oue envolvem a aplicação de operadores$ – <esolver pro"lemas utilizando estruturas de decisão$ @n t is section, &ou earn oC to use t e W>%-<5-<d*H*>%-L:e varia"le in a form action to create a single file t at olds "ot form and script, and oC to create custom error messages C en reOuired fields are not completed$

Creatin+ t.e Initia' Scri/t
As &ou did earlier in t is c apter, t e first step in creating a formHscript pair is to create t e front6 end form$ HoCever, in t is all6in6one form, t e front6end form is simpl& t e first alf of t e script and not a separate file$ . $ =pen a neC file in &our text editor$ 0 $ T&pe t e folloCing HTML: DHTMLG DH-A7G DT@TL-GAll6@n6=ne :eed"acA :ormDHT@TL-G DHH-A7G DQ=7;G / $ %tart a *H* "locA, and t en create a varia"le called Wform>"locA, C ic Cill old e entire form$ %tart Cit t e form action, and assume t at t e met od is *=%T and t e action is W>%-<5-<d*H*>%-L:e: DE Wform>"locA J F D:=<M M-TH=7JTF*=%TTF ACT@=?JTFW>%-<5-<d*H*>%-L:eTFG ?ote: Qecause &ouLre putting a long string inside a varia"le, c ances are good t at &ouLll ave a Ouotation marA or tCo$ <emem"er to escape all &our Ouotation marAs Cit a "acAslas R + $ Create an input field for t e userLs name Cit a text la"el: D*GDstrongG;our ?ame:DHstrongGD"rG D@?*)T t&peJTFtextTF ?AM-JTFsender>nameTF %@c-J/9GDH*G , $ Create an input field for t e userLs e6mail address Cit a text la"el:
@ntrodução M *H* *#gina /8 de +/

D*GDstrongG;our -6Mail Address:DHstrongGD"rG D@?*)T t&peJTFtextTF ?AM-JTFsender>emailTF %@c-J/9GDH*G 1 $ Create a text area to old t e message Cit a text la"el: D*GDstrongGMessage:DHstrongGD"rG DT-fTA<-A ?AM-JTFmessageTF C=L%J/9 <=K%J, K<A*JvirtualGDHT-fTA<-AGDH*G 2 $ Add a su"mit "utton: D*GD@?*)T T;*-JTFsu"mitTF ?AM-JTFsu"mitTF 5AL)-JTF%end T is :ormTFGDH*G 3 $ Close t e form, and t en add t e ending Ouotation marAs and instruction erminator 'semicolon(: DH:=<MGFI 8 $ Close t e *H* "locA, and t en add some more HTML so t at t e document is alid: EG DHQ=7;G DHHTMLG .9 $%ave t e file Cit t e name allinone>form$p p$ @f &ou looAed at t is code in our Ce" "roCser, &ouLd onl& see a title in t e title "ar$ T e "urning Ouestion s ould "e, FK & do Ce need all t at HTML in a varia"le called Wform>"locAEF @n t e next section, &ou add to t e script so t at it displa&s particular c unAs of code "ased on certain actions$ T e string in Wform>"locA is one of t ose c unAs$

Addin+ Error C.ec6in+ to t.e Scri/t
T e plan is to use t e glo"al varia"le W>%-<5-<d*H*>%-L:e, C ic as a value of t e scriptLs current name$ %o reall&, W>%-<5-<d*H*>%-L:e Cill ave a value of allinone>form$p p in t is instance$ K en &ou use W>%-<5-<d*H*>%-L:e as a form action, &ouLre sa&ing, FK en t e su"mit "utton is clicAed, reload t is script and do somet ing,F instead of, FK en t e su"mit "utton is clicAed, go find anot er script and do somet ing$F?oC t at &ou ave a s ell of a script, t inA a"out C at t is all6in6one script must do: 7ispla& t e form %u"mit t e form C ecA for errors *rint error messages Cit out sending t e form %end t e form if no errors are found MaAe a feC modifications to t e script to elp it determine C ic actions it s ould taAe$ @nside t e Wform>"locA varia"le, "efore t e HTML code for t e su"mit "utton, add t is line: D@?*)T t&peJTF iddenTF nameJTFopTF valueJTFdsTFG T is line creates a idden varia"le called W>*=%Tdope, C ic as a value of ds$ T e FopF stands for Foperation,F and FdsF stands for Fdo somet ing$F @ made t ese names upI t e& ave not ing to do Cit an& programming language$ ;ou can call t em C atever &ou Cant, as long as &ou understand C at t e& do 'C ic &ouLll soon see($T e W>*=%Tdope varia"le is present onl& if t e form as "een su"mitted$ %o if t e value of W>*=%Tdope is not ds, t e user asnLt seen t e form$ @f t e user asnLt seen e form, &ou need to s oC it, so add t e folloCing if]else statement "efore t e end of t e *H* "locA: if 'W>*=%Tdope RJ FdsF( ^
@ntrodução M *H* *#gina +9 de +/

HH t e& need to see t e form ec o FWform>"locAFI _ ;ouLre not done &et, "ut &our code s ould noC looA somet ing liAe t is: ;ou maAe a feC more modifications in t e next step, to add &our error messages$ @f t e form is su"mitted, t e value of W>*=%Tdope Cill "e ds, and noC &ou must account for t at$ Assume t at all t e form fields are reOuiredI after c ecAing for t e value of W>*=%Tdope, &ou c ecA for a value in all t e fields$ . $ Continue t e if]else statement: else if 'W>*=%Tdope JJ FdsF( ^ 0 $ Add an if statement Cit in t e parent statement to c ecA for values$ %tart Cit W>*=%Tdsender>namee: if 'W>*=%Tdsender>namee JJ FF( ^ / $ Create an error message for W>*=%Tdsender>namee called Wname>err: Wname>err J FDfont colorJredG*lease enter &our nameRDHfontGD"rGFI + $ %et t e value of Wsend to FnoF: Wsend J FnoFI , $ Create a similar if statement for W>*=%Tdsender>emaile: if 'W>*=%Tdsender>emaile JJ FF( ^ Wemail>err J FDfont colorJredG*lease enter &our e6mail addressRDHfontGD"rGFI Wsend J FnoFI _ 1 $ Create a similar if statement for W>*=%Tdmessagee: if 'W>*=%Tdmessagee JJ FF( ^ Wmessage>err J FDfont colorJredG*lease enter a messageRDHfontGD"rGFI Wsend J FnoFI _ 2 $ %tart an if]else statement to andle t e value of Wsend: if 'Wsend RJ FnoF( ^ HH itLs oA to sendR 3 $ Create a varia"le to old t e recipientLs e6mail address 'su"stitute &our oCn(: Wto J F&oug&ouremail$comFI 8 $ Create a varia"le to old t e su"Bect of t e e6mail: Wsu"Bect J FAll6in6=ne Ke" %ite :eed"acAFI .9 $Create a varia"le to old additional mail eaders: Wmail eaders J F:rom: M& Ke" %ite Dgenericaddressg&ourdomain$comG TnFI .. $Add to t e Wmail eaders varia"le: Wmail eaders $J F<epl&6To: W>*=%Tdsender>emaileTnFI .0 $Quild t e message string: Wmsg J F-6MA@L %-?T :<=M KKK %@T-TnFI Wmsg $J F%enderLs ?ame: W>*=%Tdsender>nameeTnFI Wmsg $J F%enderLs -6Mail: W>*=%Tdsender>emaileTnFI Wmsg $J FMessage: W>*=%TdmessageeTnTnFI
@ntrodução M *H* *#gina +. de +/

./ $Add t e mail'( function: mail'Wto, Wsu"Bect, Wmsg, Wmail eaders(I .+ $Add a simple statement to let t e user AnoC t e mail as "een sent, and lose t e if statement: ec o FD*GMail as "een sentRDHpGFI _ ., $Continue t e if]else statement to deal Cit a value of FnoF for Wsend: else if 'Wsend JJ FnoF( ^ .1 $*rint t e error messages: ec o FWname>errFI ec o FWemail>errFI ec o FWmessage>errFI .2 $*rint t e form again: ec o FWform>"locAFI .3 $Close t e current if]else "locA and t e parent if]else "locA: _ _ .8 $%ave t e file$

S b-ittin+ 0o r ,or- and Gettin+ 1es 'ts
?oC t at &ouLve created "ot a front6end form and a "acA6end script, itLs time to tr& t em out$ . $ =pen &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hallinone>form$p p$ ;ou Cill see a form containing a text field for t e personLs name, a text field for t e personLs e6mail address, a text area for t e message, and a "utton t at sa&s %end T is :orm$ 0 $ %u"mit t e form Cit out t&ping an&t ing in an& of t e fields$ T e form, Cit all t ree error messages at t e top, Cill appear in &our "roCser CindoC$ / $ T&pe &our name in t e ;our ?ame field and t en su"mit t e form$ T e form Cill reappear, t is time Cit out t e name error message$ + $ T&pe &our name in t e ;our ?ame field and &our e6mail address in t e ;our -6Mail Address field, and t en su"mit t e form$ T e form Cill reappear again, t is time Cit onl& t e message error$ , $ T&pe &our name in t e ;our ?ame field, &our e6mail address in t e ;our -6Mail Address field, and t e folloCing message:
@ntrodução M *H* *#gina +0 de +/

T is all6in6one t ing is prett& coolR 1 $ %u"mit t e form$

Savin+ t.e (a' es i* 0o 2a6e an Error
=ne t ing &ou pro"a"l& noticed in t e original script is t at if &ou made an error, t e form Cas reset and &ou lost t e values &ou ad entered$ A simple modification to t e original Wform>"locA Cill taAe care of t at pro"lem$ hust add a 5AL)- attri"ute to t e form field to old an& previous value for t e given varia"le$ . $ =pen allinone>form$p p in &our text editor$ 0 $ @nside t e Wform>"locA varia"le, modif& t e input field for ;our ?ame: D@?*)T t&peJTFtextTF ?AM-JTFsender>nameTF 5AL)-JTFW>*=%Tdsender>nameeTF %@c-J/9GDH*G / $ Modif& t e input field for ;our -6Mail Address: D@?*)T t&peJTFtextTF ?AM-JTFsender>emailTF 5AL)-JTFW>*=%Tdsender>emaileTF %@c-J/9GDH*G + $ Modif& t e text area for Message: DT-fTA<-A ?AM-JTFmessageTF C=L%J/9 <=K%J, K<A*JvirtualGW>*=%TdmessageeDHT-fTA<-AGDH*G ?ote: T ereLs no 5AL)- attri"ute for T-fTA<-A$ @nstead, t e value goes "etCeen t e start and end tags$ , $ %ave t e file, and t en open &our Ce" "roCser and t&pe ttp:HH.02$9$9$.Hallinone>form$p p$ 1 $ T&pe &our name in t e ;our ?ame field, and t en su"mit t e form$

@ntrodução M *H*

*#gina +/ de +/

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times