Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1)

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
PHP Workshop ‹#› Forms (Getting data from users).
HTML FORMS
Video, audio, embed, iframe, HTML Form
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
Forms and PHP Dr. Charles Severance
Chapter 10 Form Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Cookies, Sessions, and Authentication Dr. Charles Severance
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Unit 7 – Working with Forms 1. Creating a form 2. Accessing the submitted data 3. Common operations on forms.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
BBK P1 Module2010/11 : [‹#›] Forms (Getting data from users)
HTML Forms.
Week 9 - Form Basics Key Concepts 1. 1.Describe common uses of forms on web pages 2.Create forms on web pages using the form, input, textarea, and select.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
1 Review of Form Elements. 2 The tag Used in between tags.  Form elements(text control, buttons, etc.. ) goes here. OR  Form elements(text control,
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
+ FORMS HTML forms are used to pass data to a server. begins and ends a form Forms are made up of input elements Every input element has a name and value.
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.
Forms and PHP Dr. Charles Severance
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.
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
Introduction to Dynamic Web Content Chapter 1 Dr. Charles Severance To be used in assocition with the book: PHP, MySql, and JavaScript by Robin Nixon.
Dr. Charles Severance Using Handlebars Dr. Charles Severance
Form Processing in PHP Dr. Charles Severance
CGS 3066: Web Programming and Design Spring 2017
Forms and PHP.
How to Write Web Forms By Mimi Opkins.
Redirect, Routing, and Authentication
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
EXCEPTION HANDLING IN SERVER CLIENT PROGRAMMING
Forms Web Design Ms. Olifer.
HTML Forms Pat Morin COMP 2405.
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 ©
FORMS Explained By: Sarbjit Kaur.
Passing variables between pages
Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1)
PHP FORM HANDLING Post Method
ARUSHA TECHNICAL COLLEGE WEB DESIGN(html forms)
Web Programming– UFCFB Lecture 22
HTML/XHTML Forms 18-Sep-18.
النماذج عند الانتهاء من هذا الدرس ستكون قادرا على:
Designing Forms Lesson 10.
Introducing Forms.
HTML Forms and User Input
HTML: Basic Tags & Form Tags
CGI Programming Part II UNIX Security
Creating Form Elements
Forms and PHP.
Web Development & Design Foundations with H T M L 5
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
CNIT 131 HTML5 - Forms.
HTTP GET vs POST SE-2840 Dr. Mark L. Hornick.
Creating Forms on a Web Page
Basics of Web Design Chapter 10 Form Basics Key Concepts
Intro to Forms HTML forms are used to gather information from end-users. A form can contain elements like radio-buttons, text fields, checkboxes, submit.
HTML Forms 18-Apr-19.
Intro to Forms HTML forms are used to gather information from end-users. A form can contain elements like radio-buttons, text fields, checkboxes, submit.
© Hugh McCabe 2000 Web Authoring Lecture 8
PHP an introduction.
PHP-II.
Kanida Sinmai HTML Form Kanida Sinmai
HTML: Basic Tags & Form Tags
Web Forms.
Presentation transcript:

Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1) PHP superglobal arrays contain information about the current request, server, etc.: These are special kinds of arrays called associative arrays. Forms and PHP Super Global Arrays Array Description $_REQUEST parameters passed to any type of request $_GET, $_POST parameters passed to GET and POST requests $_FILES files uploaded with the web request $_SESSION, $_COOKIE used to identify the user, maintain sessions

Forms Submit Data http://www.php-intro.com/code/forms/form1.php <p>Guessing game...</p> <form> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess"/></p> <input type="submit"/> </form> http://www.php-intro.com/code/forms/form1.php

Forms Submit Data form1.php <p>Guessing game...</p> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess"/></p> <input type="submit"/> </form>

$_GET and $_POST PHP loads the values for the URL parameters into an array called $_GET and into an array called $_POST There is another array called $_REQUEST which merges GET and POST data

form2.php <p>Guessing game...</p> <form> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess"/></p> <input type="submit"/> </form> <pre> $_GET: <?php print_r($_GET); ?> </pre> form2.php

form3.php <p>Guessing game...</p> <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" size="40" id="guess"/></p> <input type="submit"/> </form> <pre> $_POST: <?php print_r($_POST); ?> $_GET: print_r($_GET); </pre> form3.php

Forms GET .vs. POST Two ways the browser can send parameters to the web server GET - Parameters are placed on the URL which is retrieved POST - The URL is retrieved and parameters are appended to the request in the the HTTP connection

Passing Parameters to The Server GET /form1.php?guess=42 Accept: text/html User-Agent: Lynx/2.4 libwww/2.14 Web Server HTTP Request POST /form3.php Accept: text/html User-Agent: Lynx/2.4 libwww/2.14 Content-type: application/x-www-form-urlencoded Content-length: 13 guess=42 Browser <input type="text" name="guess" id="yourid" />

Do something with these values, please HTML forms enable your web application to collect information from your users Browser Web server Server-side Programs Gimme HTML Type URL HTML for form Show form Do something with these values, please User fills out form Send values entered

Proxy Browser Servers Programs Programs Browser Programs Database If two people GET the same URL, the proxy server can GET the URL once and give the data to both. Proxy Servers Web server Database SMTP server Programs Browser Programs Browser Programs

Rules of the POST/GET Road GET is used when your are reading or searching things POST is used when data is being created or modified Web search spiders will follow GET URLs but generally not POST URLs GET Urls should be “idempotent” - the same URL should give the “same thing” each time you access it GET has an upper limit of the number of bytes of parameters and values (think about 2K)

Review: Ternary Operation <?php $oldguess = isset($_POST['guess']) ? $_POST['guess'] : ''; ?> <p>Guessing game...</p> <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" echo 'value="' . $oldguess . '"'; /></p> <input type="submit"/> </form> Persisting Form Data Across Requests form4.php Review: Ternary Operation

form4.php Hygene Alert! What happens when we use an HTML character in a form field value??

form4.php <form method="post"> <p><label>Input Guess</label> <input type="text" name="guess" id="guess "value=""><b>DIE DIE</b>" /></p> <input type="submit"/> </form>

To The Rescue: htmlentities() <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" <?php echo 'value="' . htmlentities($oldguess) . '"'; ?> /></p> <input type="submit"/> </form> form5.php

<input type="text" name="guess" id="guess" <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" <?php echo 'value="' . htmlentities($oldguess) . '"'; ?> /></p> <input type="submit"/> </form> <input type="text" name="guess" id="guess" value=""><b>DIE DIE</b>" /></p>

Processing POST Data Completely process incoming POST data (if any) - produce no output. There are many patterns for handling POST data No "rules" just "suggestions" <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> <html> <head> <title>A Guessing game</title> </head> <body style="font-family: sans-serif;"> <p>Guessing game...</p> if ( $message !== false ) { echo("<p>$message</p>\n"); <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" <?php echo 'value="' . htmlentities($guess) . '"'; /></p> <input type="submit"/> </form> </body> Produce the page output. guess.php What about frameworks?

guess.php <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> <html> <head> <title>A Guessing game</title> </head> <body style="font-family: sans-serif;"> <p>Guessing game...</p> if ( $message !== false ) { echo("<p>$message</p>\n"); <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" <?php echo 'value="' . htmlentities($guess) . '"'; /></p> <input type="submit"/> </form> </body> guess.php

guess.php <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> <html> <head> <title>A Guessing game</title> </head> <body style="font-family: sans-serif;"> <p>Guessing game...</p> if ( $message !== false ) { echo("<p>$message</p>\n"); <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" <?php echo 'value="' . htmlentities($guess) . '"'; /></p> <input type="submit"/> </form> </body> <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Nifty trick $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> <html> ... guess.php

<title>A Guessing game</title> </head> <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> <html> <head> <title>A Guessing game</title> </head> <body style="font-family: sans-serif;"> <p>Guessing game...</p> if ( $message !== false ) { echo("<p>$message</p>\n"); <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" <?php echo 'value="' . htmlentities($guess) . '"'; /></p> <input type="submit"/> </form> </body> ... ?> <html> <head> <title>A Guessing game</title> </head> <body style="font-family: sans-serif;"> <p>Guessing game...</p> <?php if ( $message !== false ) { echo("<p>$message</p>\n"); } <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" <?php echo 'value="' . htmlentities($guess) . '"'; /></p> <input type="submit"/> </form> </body>

guess.php <?php $guess = ''; $message = false; if ( isset($_POST['guess']) ) { // Nifty trick $guess = $_POST['guess'] + 0; if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?> <html> ... guess.php

guess.php <html> <head> <title>A Guessing game</title> </head> <body style="font-family: sans-serif;"> <p>Guessing game...</p> <?php if ( $message !== false ) { echo("<p>$message</p>\n"); } ?> <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" id="guess" size="40" <?php echo 'value="' . htmlentities($guess) . '"'; /></p> <input type="submit"/> </form> </body> guess.php

Other Input Types http://www.php-intro.com/code/forms/more.php Text Password Radio Button Check Box Select / Drop-Down TextArea http://www.php-intro.com/code/forms/more.php

more.php $_POST: Array ( [account] => Beth [pw] => 12345 <p>Many field types...</p> <form method="post" action="more.php"> <p><label for="inp01">Account:</label> <input type="text" name="account" id="inp01" size="40" ></p> <p><label for="inp02">Password:</label> <input type="password" name="pw" id="inp02" size="40" ></p> <p><label for="inp03">Nick Name:</label> <input type="text" name="nick" id="inp03" size="40" ></p> $_POST: Array ( [account] => Beth [pw] => 12345 [nick] => BK [when] => pm ... )

more.php $_POST: Array( ... [nick] => BK [when] => pm <p>Preferred Time:<br/> <input type="radio" name="when" value="am">AM<br> <input type="radio" name="when" value="pm" checked>PM</p> $_POST: Array( ... [nick] => BK [when] => pm [class] => si502 )

more.php <p>Classes taken:<br/> <input type="checkbox" name="class1" value="si502" checked> SI502 - Networked Tech<br> <input type="checkbox" name="class2" value="si539"> SI539 - App Engine<br> <input type="checkbox" name="class3"> SI543 - Java<br> </p> $_POST: Array( ... [when] => pm [class3] => on [soda] => 0 ) $_POST: Array( ... [when] => pm [class1] => si502 [soda] => 0 )

The values can be any string but numbers are used quite often. more.php <p><label for="inp06">Which soda: <select name="soda" id="inp06"> <option value="0">-- Please Select --</option> <option value="1">Coke</option> <option value="2">Pepsi</option> <option value="3">Mountain Dew</option> <option value="4">Orange Juice</option> <option value="5">Lemonade</option> </select> </p> $_POST: Array( ... [class] => si502 [soda] => 0 [snack] => peanuts ) The values can be any string but numbers are used quite often.

more.php $_POST: Array( ... [class] => si502 [soda] => 0 <p><label for="inp07">Which snack: <select name="snack" id="inp07"> <option value="">-- Please Select --</option> <option value="chips">Chips</option> <option value="peanuts" selected>Peanuts</option> <option value="cookie">Cookie</option> </select> </p> $_POST: Array( ... [class] => si502 [soda] => 0 [snack] => peanuts )

more.php $_POST: Array( ... <p><label for="inp08">Tell us about yourself:<br/> <textarea rows="10" cols="40" id="inp08" name="about"> I love building web sites in PHP and MySQL. </textarea> </p> $_POST: Array( ... [about] => I love building web sites in PHP and MySQL. [dopost] => Submit )

more.php $_POST: Array( ... [code] => Array ( [0] => css <p><label for="inp09">Which are awesome?<br/> <select multiple="multiple" name="code[]" id="inp09"> <option value="python">Python</option> <option value="css">CSS</option> <option value="html">HTML</option> <option value="php">PHP</option> </select> $_POST: Array( ... [code] => Array ( [0] => css [1] => html ) [dopost] => Submit

On submit input types, the text is both in the UI and in $_POST. more.php <p> <input type="submit" name="dopost" value="Submit"/> <input type="button" onclick="location.href='www.php.com'; return false;" value="Escape"> </p> $_POST: Array( ... [dopost] => Submit ) On submit input types, the text is both in the UI and in $_POST.

HTML5 Input Types HTML5 defines new input types Not all browsers support all input types The fall back to type="text" http://www.w3schools.com/html/html5_form_input_types.asp

Validation happens when you press submit. Select your favorite color: <input type="color" name="favcolor" value="#0000ff"><br/> Birthday: <input type="date" name="bday" value="2013-09-02"><br/> E-mail: <input type="email" name="email"><br/> Quantity (between 1 and 5): <input type="number" name="quantity" min="1" max="5"><br/> Add your homepage: <input type="url" name="homepage"><br> Transportation: <input type="flying" name="saucer"><br> Validation happens when you press submit.