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

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS.
PHP Workshop ‹#› Forms (Getting data from users).
1 Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Video, audio, embed, iframe, HTML Form
CHAPTER 30 THE HTML 5 FORMS PROCESSING. LEARNING OBJECTIVES What the three form elements are How to use the HTML 5 tag to specify a list of words’ form.
Objectives Using functions to organize PHP code
Intermediate Level Course. Text Format The text styles, bold, italics, underlining, superscript and subscript, can be easily added to selected text. Text.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2014 All Rights Reserved.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Python and Web Programming
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Session 2 Tables and forms in HTML Adapted by Sophie Peter from original document by Charlie Foulkes.
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
Unit 7 – Working with Forms 1. Creating a form 2. Accessing the submitted data 3. Common operations on forms.
ITM © Port, Kazman1 ITM 352 HTML Forms, Basic Form Processing.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Lesson 13: Building Web Forms Introduction to Adobe Dreamweaver CS6 Adobe Certified Associate: Web Communication using Adobe Dreamweaver CS6.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
PHP - Part 2. More operators... Arithmetic and Assignment Operators e.g., using + and =  $IntA=5; $intB=8; $intC=$intA+$intB; //intC is 13  // Same.
BBK P1 Module2010/11 : [‹#›] Forms (Getting data from users)
HTML Forms.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Creating Web Page Forms. Introducing Web Forms Web forms collect information from users Web forms include different control elements including: –Input.
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Session 8: Working with Form iNET Academy Open Source Web Development.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
Web Page Design Forms! Website Design. Objectives What forms can do The Attributes of the form tag Using Textboxes Textareas Checkboxes Radio buttons.
CGI in Context CGI is Common Gateway Interface, supporting a greater degree of interaction between the user and a Web page, most commonly by means of a.
Basic HTML.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
Copyright (c) 2004 Prentice-Hall. All rights reserved. 1 Committed to Shaping the Next Generation of IT Experts. Project 6: Creating XHTML Forms Kelly.
1 HTML forms (cont.)
NMD202 Web Scripting Week2. Web site
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
Form Data (part 2) MIS 3502, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015 Slide 1.
CGS 3066: Web Programming and Design Spring 2016 PHP.
Web Forms. Web Forms: A form allows our web visitors to submit information to us. Some examples uses for forms are to let the web user contact us, fill.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
Web Programming with PHP (3) Superglobals, Form & File processing.
Simple PHP Web Applications Server Environment
Receiving form Variables
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
DBW - PHP DBW2017.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
HTML Forms and User Input
Lesson 6: Web Forms.
PHP-II.
Presentation transcript:

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

Chapter Eleven Working With Forms ses/php/slides/chapter11.ppt 2

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

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

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

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

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

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

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: Superglobal Variable $_SERVER 9

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: ) Gecko/ 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 Kelvin To Centigrade: Kelvin – 273 Fahrenheit To Kelvin: 5/9 * (Fahrenheit - 32) Kelvin To Fahrenheit: ((Kelvin - 273) * 9/5 ) + 32 Student Exercise

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

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

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

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

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

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

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

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

to be continued... php/slides/chapter17a.strcmp.ppt 41