Superglobal Variable: $GLOBALS"> Superglobal Variable: $GLOBALS">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.

Similar presentations


Presentation on theme: "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved."— Presentation transcript:

1 Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com Copyright © 2010 All Rights Reserved.

2 Chapter Eleven Working With Forms http://webcert.kirkwood.edu/~fmcclurg /courses/php/slides/chapter11.ppt

3 Description: Superglobals are built-in variables that are global in scope. These variables are available throughout the entire script. $GLOBALS : Hash containing references to all variables defined in global scope. Example: <?php function initialize( $key, $value ) { // define global variable $GLOBALS[$key] = $value; } initialize( "foo", "It's every where!" ); echo( $GLOBALS["foo"] ); ?> Superglobal Variable: $GLOBALS

4 Description: The PHP mechanism for extracting operating system variables is performed via $_ENV: $_ENV: Hash containing system environment variables. Example via “set” (Windows) or “printenv” (Unix): Superglobal Variable: $ _ ENV

5 Note: If the array $_ENV is undefined, you may need change the following setting in your “php.ini” file and restart the web server. For xampp on windows, this file is found in “ C:\xampp\php\php.ini ” Check phpinfo() to verify the exact location of this file. Change this line: variables_order = "GPCS" To this instead: variables_order = "GPCSE" “php.ini” directive concerning $ _ ENV

6 Summary: Write a program that displays the key/value pairs contained in the superglobal associative array “$_ENV”. Requirements: 1.Bold face the variable name and the key. 2.Place a paragraph between each key/value pair. Output should be similar to the following format: $_ENV[key1]: value1 $_ENV[key2]: value2... Student Exercise 11.1

7 Here is one way to do it (see file “ _ ENV.1.php”): <?php $env = $_ENV; // copy original ksort( $env ); // sort keys foreach ( $env as $key => $val ) { printf( " \$_ENV[%s]: %s \n", $key, $val ); } ?> Student Solution 11.1 (option 1)

8 Here is another way to do it (see file “_ENV.2.php”): <?php $env = $_ENV ; // copy original ksort( $env ); // sort keys foreach ( $env as $key => $val ) { ?> $_ENV[ ]: <?php } ?> Student Solution 11.1 (option 2)

9 Note: If “ ” is not working, you may need change the following setting in your “php.ini” file and restart the web server. For xampp on windows, this file is found in “ C:\xampp\php\php.ini ” Check the phpinfo() call for the exact location of this file. Change this line: short_open_tag = Off To this instead: short_open_tag = On “php.ini” directive concerning “php.ini” directive concerning

10 Description: The PHP mechanism for extracting information from the web server is performed via the superglobal variable $_SERVER The following are some of the more significant keys contained in this variable: DOCUMENT_ROOT : Full pathname of the document root on the server (example: /opt/lampp/htdocs) HTTP_HOST : Server hostname (example: localhost) SERVER_NAME : Server domain name (example: www.example.com) Superglobal Variable $ _ SERVER

11 HTTP_REFERER : Page previous to current page. Note: Can not always be trusted. HTTP_USER_AGENT : OS and browser of client (ex: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5) Gecko/20091109 Ubuntu/9.10 (karmic) Firefox/3.5.5) PHP_SELF : Filename of current script. (ex: /~frmcclurg/courses/php/examples/chapter11/_SER VER.php QUERY_STRING : String containing the form variables and values (ex: ?q=ubuntu) More Superglobal $ _ SERVER

12 Description: The PHP mechanism for extracting information from a form is performed via the following predefined superglobal variables. $_GET : Hash that is populated upon submitting a form via the GET method. $_POST : Hash that is populated upon submitting a form via the POST method. $_REQUEST : Hash that is populated with the contents of $_GET and $_POST (and some other variables). Superglobal Variables for Forms

13 Summary: Write a program that displays the key/value pairs contained in the super global associative array “$_SERVER”. Requirements: 1.Bold face the variable name and the key and place on a separate line. 2.The value should be indented and placed on a separate line. 3.Place a paragraph between each key/value pair. Output should be in the following format: $_SERVER[key1]: value1 $_SERVER[key2]: value2... Student Exercise 11.2 (extra credit)

14 Here is one way to do it (see file “_SERVER.php”): <?php $server = $_SERVER; // copy original ksort( $server ); // sort keys foreach ( $server as $key => $val ) { printf( " \$_SERVER[%s]: %s \n", $key, $val ); } ?> Student Solution 11.2

15 Description: HTML forms provide a way to obtain input from the user. That information can then be used in a calculation or stored in a database. <form action="search.html" method="GET"> Search: <input type="text" name="search" value="" /> <input type="submit" value="Find!" /> HTML Search Form Example

16 Description: It is often convenient for a form to “remember” the text from a previous submission and set it as the default value for the next submission. In this way, the user does not have to re-enter the same information again. Solution: Any form element can become “sticky” by using the results of the previous submission via the $_GET, $_POST, or $_REQUEST hash. Making a “Sticky” Form

17 Description: Only a few changes may be necessary to convert a form from HTML to PHP. The following code makes the form self processing and “sticky” (see file “search.php”): " method="GET"> Search: <input type="text" name="find" value=" " /> <input type="submit" value="Find!" /> PHP Search Form Example

18 Summary: Write a program that prints on the bottom of the form, the same text string that is contained in the text widget. The string should be displayed upon form submission. Hint: The solution can be obtained by adding less than a dozen lines to the file “search.php” Student Exercise 11.3

19 The following lines should be added after the form in the file “search.php” (see file “searchEcho.php”):... <?php if ( isset( $_REQUEST['find'] ) ) { printf( "You submitted: \"%s\"", $_REQUEST['find'] ); } ?> Student Solution 11.3

20 Problem: Checkboxes and section lists can have more than one value selected. A variable can only store one value at a time. (see file “multiWrong.php”) " method="GET"> <input type="checkbox" name="food" value="Hamburger" /> Hamburger Fries Drink... Problem with Multiple Values (pg 1)

21 Problem: Checkboxes and section lists can have more than one value selected. A variable can only store one value at a time. (see file “multiWrong.php”)... <?php if ( $_GET ) { printf( " " ); printf( "Your order is: " ); foreach ( $_GET as $name => $value ) { printf( "\$_GET[%s]: %s ", $name, $value ); } ?> Problem with Multiple Values (pg 2)

22 Solution: An array is used to store multiple values. (see file “multiRight.php”) " method="GET"> <input type="checkbox" name="food[]" value="Hamburger" /> Hamburger <input type="checkbox" name="food[]" value="Fries" /> Fries <input type="checkbox" name="food[]" value="Drink" /> Drink Solution with Multiple Values (pg 1)

23 Solution: An array is used to store multiple values. (see file “multiRight.php”) <?php if ( $_GET['food'] ) { printf( " " ); printf( "Your order: " ); for ( $i = 0; $i < count( $_GET['food'] ); $i++) { $value = $_GET['food'][$i]; printf( "\$_GET['food'][%d]: %s ", $i, $value ); } ?> Solution with Multiple Values (pg 2)

24 Step 1: Create a form that contains a multiple selection list box and one checkbox similar to the following: Student Exercise 11.4 (pg 1)

25 Step 2: Write a program that will display all the key/value pairs contained in the super global associative array “$_REQUEST” upon pressing the submit button. Hint: The syntax for a selection box is as follows: <select name="widgetName" multiple="multiple"> List Item Label Here... Student Exercise 11.4 (pg 2)

26 Here is one way to do it (see file “multiSelect.php”): " method="GET"> Used? Hard Cover Soft Cover Audio Book Video Student Solution 11.4 (pg 1)

27 Here is one way to do it (for a complete file listing, see file “multiSelect.php”): <?php if ( isset( $_REQUEST['order'] ) ) { printf( " You selected: \n" ); foreach ( $_REQUEST as $key => $outie ) { if ( is_array( $_REQUEST[$key] ) ) { $i = 0; // reset counter foreach ( $_REQUEST[$key] as $innie ) { printf( " \$_REQUEST[%s][%d]: %s \n", $key, $i, $innie); $i++; } // end foreach } // end if else { printf( "\$_REQUEST[%s]: %s \n", $key, $outie); } // end else } // end foreach } // end if ?> Student Solution 11.4 (pg 2)

28 Summary: Select one formula below. Create a form that allows a user to type the text value of a temperature and output its equivalent value in another unit upon form submission. Extra credit: Select between multiple conversions using a radio button. The conversion formulas are as follows: Fahrenheit To Centigrade: 5/9 * (Fahrenheit - 32) Centigrade To Fahrenheit: (9/5 * Centigrade) + 32 Centigrade To Kelvin: Centigrade + 273 Kelvin To Centigrade: Kelvin – 273 Fahrenheit To Kelvin: 5/9 * (Fahrenheit - 32) + 273 Kelvin To Fahrenheit: ((Kelvin - 273) * 9/5 ) + 32 Student Exercise 11.4

29 Here is one way to do it (see file “tempConvert.php”): " method="GET"> <input type="text" name="degrees" value=" " /> <input type="submit" name="doit" value="Convert" />... Student Solution 11.4 (option 1, pg 1)

30 Here is one way to do it (see file “tempConvert.php”):... <?php if ( isset( $_GET['doit'] ) ) { printf( " \n" ); $input = $_GET['degrees']; /* Fahrenheit To Centigrade */ $output = 5/9 * ($input - 32); $inputUnits = "Fahrenheit"; $outputUnits = "Centigrade"; printf( "%f° %s = %f° %s", $input, $inputUnits, $output, $outputUnits ); } ?> Student Solution 11.4 (option 1, pg 2)

31 Here is another way to do it (see file “multiConvert.php”): Student Solution 11.4 (option 2, pg 1)

32 Here is another way to do it (see file “multiConvert.php”): " method="GET"> <input type="radio" name="convert" value="f2c" <?= StickyRadio( $_GET['convert'], 'f2c' ) ?> /> Fahrenheit To Centigrade... <input type="text" name="degrees" value=" " /> <input type="submit" name="doit" value="Convert" />... Student Solution 11.4 (option 2, pg 2)

33 Here is another way to do it (for a complete listing, see file “multiConvert.php”):... <?php if ( isset( $_GET['doit'] ) ) { printf( " \n" ); $input = $_GET['degrees']; $convert = $_GET['convert']; /* conversion formulas */ if ( $convert == "f2c" ) /* Fahrenheit To Centigrade */ $output = 5/9 * ($input - 32); elseif ( $convert == "c2f" ) /* Centigrade To Fahrenheit */ $output = (9/5 * $input) + 32;... Student Solution 11.4 (option 2, pg 3)

34 Here is another way to do it (see file “multiConvert.php”):... <?php if ( isset( $_GET['doit'] ) ) {... /* units of input */ if ( preg_match('/^f/', $convert) ) $inputUnits = "Fahrenheit"; elseif ( preg_match('/^c/', $convert)) $inputUnits = "Centigrade"; elseif ( preg_match('/^k/', $convert)) $inputUnits = "Kelvin";... Student Solution 11.4 (option 2, pg 4)

35 Here is another way to do it (see file “multiConvert.php”):... <?php if ( isset( $_GET['doit'] ) ) {... /* units of output */ if ( preg_match( '/f$/', $convert ) ) $outputUnits = "Fahrenheit"; elseif ( preg_match( '/c$/', $convert ) ) $outputUnits = "Centigrade"; elseif ( preg_match( '/k$/', $convert ) ) $outputUnits = "Kelvin";... Student Solution 11.4 (option 2, pg 5)

36 Here is another way to do it (see file “multiConvert.php”):... <?php if ( isset( $_GET['doit'] ) ) {... /* display calculations */ printf( "%f° %s = %f° %s", $input, $inputUnits, $output, $outputUnits ); } // end if ?> Student Solution 11.4 (option 2, pg 6)

37 to be continued... http://webcert.kirkwood.edu/~fmcclurg/c ourses/php/slides/chapter17a.strcmp.ppt


Download ppt "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved."

Similar presentations


Ads by Google