Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

PHP Programming with MySQL (second edition) by Don Gosselin, Diana Kokoska, obert !asterbrooks ("S#$%&'( )*+%,% -'+*%.-+.

%,, "S#$%&,( ,%-'+*%.-+.%')


e/erence( htt0(11www20h02net1man3al1en1langre/20h0 4ha0ter . 5 Handling 6ser in03t &2 4reate a 7eb /orm to hel0 in creating 893mble: 03;;les2 4reate a /orm that has /o3r in03t /ields named 7ord&, 7ord<, 7ord', and 7ord., as well as 8 eset: and 8S3bmit: b3ttons2 4reate a /orm 0rocessing scri0t that =eri/ies that all /o3r words are entered, and that all /o3r are between . and * characters long2 >nce all the words ha=e been =eri/ied as correct, 3se the strto300er() and str?sh3//le() /3nctions to 0rod3ce /o3r @3mbled sets o/ letters2 (two%0art /orm) a2 4reate a new doc3ment in yo3r teAt editor2 By0e the CDD>4BEP!F declaration, ChtmlF element, header in/ormation, and CbodyF element2 b2 Gdd the /ollowing HHBML /orm tags in the doc3ment body( C/orm actionIJcha0ter?,.?hw?,&?0rocess20h0J methodIJ0ostJF 7ord &( Cin03t ty0eIJteAtJ nameIJ7ord&J 1FCbr 1F 7ord <( Cin03t ty0eIJteAtJ nameIJ7ord<J 1FCbr 1F 7ord '( Cin03t ty0eIJteAtJ nameIJ7ord'J 1FCbr 1F 7ord .( Cin03t ty0eIJteAtJ nameIJ7ord.J 1FCbr 1F Cin03t ty0e IJresetJ =al3e I J4lear KormJ 1FLnbs0M Lnbs0MCin03t ty0eIJs3bmitJ nameIJS3bmitJ =al3eIJSend KormJ 1F C1/ormF c2 Sa=e the doc3ment as cha0ter?,.?hw?,&2html2 d2 4reate a new doc3ment in yo3r teAt editor2 By0e the CDD>4BEP!F declaration, ChtmlF element, header in/ormation, and CbodyF element2 e2 Gdd the o0ening and closing tags /or the PHP scri0t section in the body o/ the doc3ment( CN0h0 NF /2 Gdd the dis0lay!rror() /3nction to the scri0t section2 Bhis /3nction dis0lays the error message, and takes two 0arameters( O/ield$ame, which is the name o/ the /ield as it a00ears on the 7eb /ormM and OerrorMsg, which describes the error /or the 3ser2 Bhere is no ret3rn =al3e /or this /3nction2 /3nction dis0lay!rror(O/ield$ame, OerrorMsg) P global Oerror4o3ntM echo J!rror /or QJO/ield$ameQJ( OerrorMsgCbr 1FJM RROerror4o3ntM

S g2 4reate a second /3nction called =alidate7ord() below the dis0lay!rror() /3nction2 Bhis /3nction takes two 0arameters2 Bhe /irst 0arameter, Odata, is a string to be =alidated2 Bhe second 0arameter, O/ield$ame, is the name o/ the /orm /ield2 Bhe /3nction ret3rns the Odata 0arameter a/ter it has been cleaned 302 $otice that the /3nction 3ses the global =ariable Oerror4o3nt2 /3nction =alidate7ord(Odata, O/ield$ame) P global Oerror4o3ntM i/ (em0ty(Odata)) P dis0lay!rror(O/ield$ame, JBhis /ield is reT3iredJ)M Oret=al I JJM S else P 11 only clean 30 the in03t i/ it isnUt em0ty Oret=al I trim(Odata)M Oret=al I stri0slashes(Oret=al)M i/ ((strlen(Oret=al) C .) VV (strlen(Oret=al) F *)) P dis0lay!rror(O/ield$ame, J7ords m3st be at least /o3r and at most se=en letters longJ)M S i/ (0reg?match(J1WXa%;YRO1iJ, Oret=al) II ,) P dis0lay!rror(O/ield$ame, J7ords m3st be only lettersJ)M S S Oret=al I strto300er(Oret=al)M Oret=al I str?sh3//le(Oret=al)M ret3rn(Oret=al)M S h2 "mmediately a/ter the =alidate7ord() /3nction, declare and initiali;e a new =ariable called Oerror4o3nt and a new array called OwordsXY as /ollows( Oerror4o3nt I ,M Owords I array()M i2 Gdd assignment statements /or the Owords array =ariable to recei=e the o3t03t o/ the =alidate7ord() /3nction /or each /orm /ield( OwordsXY I =alidate7ord(O?P>SBXU7ord&UY, J7ord &J)M OwordsXY I =alidate7ord(O?P>SBXU7ord<UY, J7ord <J)M OwordsXY I =alidate7ord(O?P>SBXU7ord'UY, J7ord 'J)M OwordsXY I =alidate7ord(O?P>SBXU7ord.UY, J7ord .J)M @2 Gdd a conditional statement immediately a/ter the =al3es o/ Owords ha=e been assigned2 Bhis statement will dis0lay the total n3mber o/ errors /o3nd or the sh3//led words i/ there were no errors2

i/ (Oerror4o3nt F ,) P echo JPlease 3se the QJ#ackQJ b3tton to re%enter the data2Cbr 1FJM S else P Owordn3m I ,M /oreach (Owords as Oword) P echo J7ord J 2 RROwordn3m 2 J( OwordCbr 1FJM S S k2 Sa=e the 0rogram as cha0ter?,.?hw?,&?0rocess20h02 l2 Best the 0rogram2 <2 4reate an Gll%in%>ne /orm that calc3lates an em0loyeeZs weekly gross salary, based on the n3mber o/ ho3rs worked and an ho3rly wage that yo3 choose2 a2 4reate a new doc3ment in yo3r teAt editor2 By0e the CDD>4BEP!F declaration, ChtmlF element, header in/ormation, and CbodyF element2 b2 Gdd the /ollowing scri0t section to the doc3ment body( CN0h0 NF c2 4reate and initiali;e a #oolean =ariable called ODis0layKorm, which will be 3sed to determine i/ the 7eb /orm sho3ld be redis0layed, and two string =ariables called OHo3rs7orked and OHo3rly ate( ODis0layKorm I B 6!M OHo3rs7orked I JJM OHo3rly ate I JJM d2 Gdd the /ollowing code to check whether the /orm data has been entered2 "/ it has, the data will be =alidated( i/ (isset(O?P>SBXUS3bmitUY)) P OHo3rs7orked I O?P>SBXUHo3rs7orkedUYM OHo3rly ate I O?P>SBXUHo3rly ateUYM i/ (is?n3meric(OHo3rs7orked) LL is?n3meric(OHo3rly ate)) P ODis0layKorm I KGLS!M S else P echo JC0F$3mber o/ ho3rs worked and Ho3rly rate are n3meric =al3es2C10FJM ODis0layKorm I B 6!M S S

e2 Gdd the /ollowing code to dis0lay the /orm, incl3ding the entered =al3e /or the n3mber o/ ho3rs worked and ho3rly rate /ields2 $ote the 3se o/ ad=anced embedding o/ HHBML2 i/ (ODis0layKorm) P NF C/orm nameIJ7eeklySalaryJ actionIJcha0ter?,.?hw?,<20h0J methodIJ0ostJF C0F$3mber o/ Ho3rs worked( Cin03t ty0eIJteAtJ nameIJHo3rs7orkedJ =al3eIJCN0h0 echo OHo3rs7orkedM NFJ 1FC10F C0FHo3rly ate( Cin03t ty0eIJteAtJ nameIJHo3rly ateJ =al3eIJCN0h0 echo OHo3rly ateM NFJ 1FC10F C0FCin03t ty0eIJresetJ =al3eIJ4lear KormJ 1FLnbs0M Lnbs0MCin03t ty0eIJs3bmitJ nameIJS3bmitJ =al3eIJSend KormJ 1FC10F C1/ormF CN0h0 S /2 Gdd an else cla3se to 3se the /orm data once it is entered correctly, as /ollows( else P echo JC0FBhank yo3 /or com0lete yo3r weekly salary /orm2C10FJM i/ (OHo3rs7orked F .,) P O!AtraPay I (OHo3rs7orked % .,) [ &2- [ OHo3rly ateM O eg3larPay I ., [ OHo3rly ateM S else P O!AtraPay I ,M O eg3larPay I OHo3rs7orked [ OHo3rly ateM S OBotal7eeklyPay I O eg3larPay R O!AtraPayM echo JC0FEo3r 7eekly Gross Pay is( J 2 ro3nd(OBotal7eeklyPay, <) 2 JC10FJM echo JC0F$3mber o/ Ho3rs worked( OHo3rs7orked2C10FJM echo JC0FHo3rly ate( OHo3rly ate2C10FJM echo JCbr 1FCbr 1FC0FCa hre/IQJcha0ter?,.?hw?,<20h0QJFBry againN C1aFC10FJM S g2 Sa=e the doc3ment as cha0ter?,.?hw?,<20h0, and Best the 0rogram2

You might also like