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

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Chapter Eleven Working With Forms http://cecert.kirkwood.edu/~fmcclurg/cour ses/php/slides/chapter11.ppt 2

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 3

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 4

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 5

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 6

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) 7

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) 8

9 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 9

10 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 10

11 Summary: Write a program that displays the key/value pairs contained in the Superglobal 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) 11

12 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 12

13 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

14 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"> Find: <input type="text" name="find" value="" /> <input type="submit" name="search" value="Search Data" /> HTML Search Form Example 14

15 Description: When the submit button is pressed the Superglobal variables $_GET, $_POST or $_REQUEST are defined with key/value pairs: Find: <input type="submit" name="search" value="Search Database" /> <?php foreach ( $_GET as $key => $val ) { printf( " \$_GET[%s]: %s \n", $key, $val ); } ?> Extracting form values in PHP 15

16 Description: When the submit button is pressed the Superglobal variables $_GET, $_POST or $_REQUEST are defined with key/value pairs: Favorite Animal: Favorite Food: Lasagna Pizza Favorite Color: Red Green Blue <?php foreach ( $_POST as $key => $val ) { printf( " \$_POST[%s]: %s \n", $key, $val ); } ?> Extracting more form values in PHP 16

17 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

18 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"> Find: <input type="text" name="find" value=" " /> <input type="submit" value="Search" /> PHP Search Form Example 18

19 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 19

20 Description: PHP allows you to control which errors are reported: <?php // Change error reporting to display // 'used before initialized' variables // error_reporting( E_ALL | E_NOTICE ); // Change to turn off error reporting // so it does not display 'used before // initialized' variables error_reporting( E_ALL ^ E_NOTICE ); ?> PHP Error Reporting 20

21 Description: The isset() function is a good way to determine if the $_GET has been populated.... <form action=" " method="GET"> Search: <input type="text" name="find" value="<?= isset( $_GET['find'] ) ? $_GET['find'] : '' ?>" /> <input type="submit" value="Find!" />... Protecting from initialization errors 21

22 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 22

23 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 23

24 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) 24

25 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 $key => $value ) { printf( "\$_GET[%s]: %s ", $key, $value ); } ?> Problem with Multiple Values (pg 2) 25

26 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 <input type="submit" name="order" value="Order" />... Solution with Multiple Values (pg 1) 26

27 Solution: An array is used to store multiple values. (see file “multiRight.php”)... <?php if ( $_GET ) { printf( " " ); printf( "Your order: " ); foreach ( $_GET as $key => $outie ) { if ( ! is_array( $_GET[$key] ) ) { printf( "\$_GET[%s]: %s ", $key, $outie ); } else // $_GET[$key] is an array { foreach ( $_GET[$key] as $innie ) { printf( "\$_GET[%s]: %s ", $key, $innie ); } ?> Solution with Multiple Values (pg 2) 27

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

29 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) 29

30 Here is one way to do it (see file “multiSelect.php”): " method="GET"> Hard Cover Soft Cover Audio Book Video <input type="submit" name="order“ value="Order" />... Student Solution 11.4 (pg 1) 30

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

32 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 32

33 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) 33

34 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) 34

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

36 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) 36

37 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) 37

38 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) 38

39 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) 39

40 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) 40

41 to be continued... http://cecert.kirkwood.edu/~fmcclurg/courses/ php/slides/chapter17a.strcmp.ppt 41


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

Similar presentations


Ads by Google