[ Pobierz całość w formacie PDF ] .Give it acalculate() method that prints the numbers to the browser.Create a class called addCalc() that inherits its functionality from baseCalc().Override the calculate() method so that the sum of the properties is printed tothe browser.Repeat activity 2, for a class called minusCalc().Give minusCalc() a calculatemethod that subtracts the first property from the second, outputting the resultto the browser.154Hour 9: Working with FormsOverviewUntil now, all examples in this book have been missing a crucial dimension.You canset variables and arrays, create and call functions, and work with objects.All this ismeaningless if users can't reach into a language's environment to offer itinformation.In this hour, you will look at strategies for acquiring and working withuser input.On the World Wide Web, HTML forms are the principal means by which substantialamounts of information can pass from the user to the server.PHP is designed toacquire and work with information submitted via HTML forms.In this hour, you will learnHow to get and use environment variablesHow to access information from form fieldsHow to work with form elements that allow multiple selectionsHow to create a single document that contains both an HTML form and the PHPcode that handles its submissionHow to save state with hidden fieldsHow to redirect the user to a new pageHow to build HTML forms that upload files and how to write the PHP code tohandle themGlobal and Environment VariablesBefore you actually build a form and use it to acquire data, you need to make a smalldetour and look again at global variables.You first met these in Hour 6, "Functions."A global variable is any variable declared at the "top level" of a script that is,declared outside a function.All functions are made available in a built-in associativearray called $GLOBALS.This is useful in Listing 9.1 because we can take a peek atall our script's global variables with a single loop.Listing 9.1: Looping Through the $GLOBALS Array1:2:3: Listing 9.1 Looping through the $GLOBALS array4:1555:6: $value )11: {12: print "\$GLOBALS[\"$key\"] == $value";13: }14: ?>15:16:We declare three variables and then loop through the built-in $GLOBALS associativearray, writing both array keys and values to the browser.In the output, we wereable to locate the variables we defined, but we saw an awful lot more besides these.PHP automatically defines global variables that describe both the server and clientenvironments.These are known, therefore, as environment variables.According toyour system, server, and configuration, the availability of these variables will vary,but they can be immensely useful.Table 9.1 lays out some common environmentvariables.These can be accessed as part of the $GLOBALS array, or directly.Table 9.1: Environment VariablesVariable Contains Example$HTTP_USER_AGENT The name and Mozilla/4.6version of the (X11;I;Linux2.2.6-15apmac ppc)client$REMOTE_ADDR The IP address 158.152.55.35of the client$REQUEST_METHOD Whether the POSTrequest wasGET or POST$QUERY_STRING For GET name=matt&address=unknownrequests, theencoded datasend appendedto the URL$REQUEST_URI The full address /matt/php-156of the request book/forms/eg9.14.html?including query name=mattstring$HTTP_REFERER The address of http://www.test.com/a_page.htmlthe page fromwhich therequest wasmadeIn addition to environment variables, PHP makes some other global variablesavailable to you.The variable $GLOBALS["PHP_SELF"], for example, gives you thepath to the script currently running.On my system this was as follows:/matt/php-book/forms/eg9.1.phpThis variable can also be directly accessed as the global variable $PHP_SELF.Thiswill be useful in many of the examples in this hour.We will often include the HTMLforms we use in the same page as the PHP code that analyzes the content theysubmit.We can use $PHP_SELF as the string assigned to the HTML FORM element'sACTION argument, saving us the trouble of hard-coding the name of the page.PHP's $GLOBALS array will become useful in other ways as well.A Script to Acquire User InputFor now, we'll keep our HTML separate from our PHP code.Listing 9.2 builds asimple HTML form.Listing 9.2: A Simple HTML Form1:2:3: Listing 9.2 A simple HTML form4:5:6:7:8:9:10:11:12:13:14:15:157We define a form that contains a text field with the name "user", a text area with thename "address", and a submit button.It is beyond the remit of this book to coverHTML in detail
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|