Forms and PHP Dr. Charles Severance www.php-intro.com.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

HTML Forms. collect information for passing to server- side processes built up from standard widgets –text-input, radio buttons, check boxes, option lists,
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?
Introduction to PHP Dr. Charles Severance
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.
Tutorial 6 Creating a Web Form
Lecture 14 HTML Forms CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Web Database Programming Input Validation. User Input on the Web Web browser built-in mechanisms –HTML Forms HTTP POST method –Hyperlinks HTTP GET method.
How the web works: HTTP and CGI explained
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
Chapter 10 Form Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Cookies, Sessions, and Authentication Dr. Charles Severance
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Week 4  Using PHP with HTML forms  Form Validation  Create your own Contact form Please Visit:
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.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Website Development with PHP and MySQL Saving Data.
HTML Forms.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
HTML - Forms By Joaquin Vila, Ph.D.. Form Tag The FORM tag specifies a fill-out form within an HTML document. More than one fill-out form can be in a.
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.
HTML FORMS GET/POST METHODS. HTML FORMS HTML Forms HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes,
HTTP - Forms - AppEngine Jim Eng
PHP Arrays Dr. Charles Severance
IS2802 Introduction to Multimedia Applications for Business Lecture 5: JavaScript, form validation and browser detection Rob Gleasure
Google Application Engine Introduction Jim Eng with thanks to Charles Severance
Introduction to Dynamic Web Content Dr. Charles Severance
Operating Systems Lesson 12. HTTP vs HTML HTML: hypertext markup language ◦ Definitions of tags that are added to Web documents to control their appearance.
PHP Functions Dr. Charles Severance
HTML Forms Forms, Controls, Fields, Inputs, Submission, Validation SoftUni Team Technical Trainers Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
Working with Forms in PHP HTTP GET / POST, Validation, Escaping, Input Types, Submitting Arrays, URL Redirecting, PHP Superglobals Svetlin Nakov Technical.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
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.
Department of Computer Science, Florida State University CGS 3066: Web Programming and Design Spring Forms, HTML5 layout.
Forms and PHP Dr. Charles Severance
PHP Arrays Dr. Charles Severance
PHP Functions / Modularity Dr. Charles Severance
Tutorial 6 Creating a Web Form
HTML Elements: Forms and Controls Chapter 9 B. Ramamurthy.
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 Dr. Charles Severance
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
Introduction to Dynamic Web Content
PHP Arrays Dr. Charles Severance
Our Technologies Dr. Charles Severance
Forms and PHP.
Getting PHP Hosting Dr. Charles Severance
Using JSON Dr. Charles Severance
Redirect, Routing, and Authentication
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
HTTP Parameters and Arrays
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 ©
PHP Arrays Dr. Charles Severance
Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1)
Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1)
Web Programming– UFCFB Lecture 22
Validation and Building Small Apps
Forms and PHP.
Introduction to Dynamic Web Content
Using jQuery Dr. Charles Severance
Introduction to Dynamic Web Content
Model View Controller (MVC)
Presentation transcript:

Forms and PHP Dr. Charles Severance

Forms Submit Data Guessing game... Input Guess

Forms Submit Data Guessing game... Input Guess form1.php

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

Guessing game... Input Guess $_GET: <?php print_r($_GET); ?> form2.php

Guessing game... Input Guess $_POST: <?php print_r($_POST); ?> $_GET: <?php print_r($_GET); ?> 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 /simpleform.html?yourname=fred Accept: www/source Accept: text/html User-Agent: Lynx/2.4 libwww/2.14 POST /simpleform.html Accept: www/source Accept: text/html User-Agent: Lynx/2.4 libwww/2.14 Content-type: application/x-www-form-urlencoded Content-length: 13 yourname=fred HTTP Request Browser Web Server

What does that mean? 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)

<?php $oldguess = isset($_POST['guess']) ? $_POST['guess'] : ''; ?> Guessing game... Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. $oldguess. '"'; ?> /> form4.php Review: Ternary Operation

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

Input Guess DIE DIE " /> form4.php

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

Input Guess <input type="text" name="guess" id="guess" <?php echo 'value="'. htmlentities($oldguess). '"'; ?> /> <input type="text" name="guess" id="guess" value=""><b>DIE DIE</b>" />

Processing POST Data 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..."; } ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"'; ?> /> Completely process incoming POST data (if any) - produce no output. 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..."; } ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"'; ?> />

<?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..."; } ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"'; ?> /> <?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..."; } ?>... 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..."; } ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"'; ?> />... ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"'; ?> />

<?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..."; } ?>... guess.php

A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" <?php echo 'value="'. htmlentities($guess). '"'; ?> /> guess.php

Other Input Types Text Password Radio Button Check Box Select / Drop-Down TextArea

Many field types... Account: Password: Nick Name: more.php $_POST: Array ( [account] => Beth [pw] => [nick] => BK [when] => pm... )

Preferred Time: AM PM more.php $_POST: Array(... [nick] => BK [when] => pm [class] => si )

Classes taken: SI502 - Networked Tech SI539 - App Engine SI543 - Java more.php $_POST: Array(... [when] => pm [class1] => si502 [soda] => 0... ) $_POST: Array(... [when] => pm [class3] => on [soda] => 0... )

Which soda: -- Please Select -- Coke Pepsi Mountain Dew Orange Juice Lemonade more.php $_POST: Array(... [class] => si502 [soda] => 0 [snack] => peanuts... ) The values can be any string but numbers are used quite often.

Which snack: -- Please Select -- Chips Peanuts Cookie more.php $_POST: Array(... [class] => si502 [soda] => 0 [snack] => peanuts... )

Tell us about yourself: I love building web sites in PHP and MySQL. more.php $_POST: Array(... [about] => I love building web sites in PHP and MySQL. [dopost] => Submit... )

Which are awesome? Python CSS HTML PHP more.php $_POST: Array(... [code] => Array ( [0] => css [1] => html ) [dopost] => Submit... )

<input type="button" onclick="location.href=' return false;" value="Escape"> more.php $_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" sp sp

Select your favorite color: Birthday: Quantity (between 1 and 5): <input type="number" name="quantity" min="1" max="5"> Add your homepage: Transportation: Validation happens when you press submit.

Summary Forms, $_GET and $_POST Sanitizing HTML Model-View Controller Form fields New form fields in HTML5

Acknowledgements / Contributions These slides are Copyright Charles R. Severance ( as part of and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Continue new Contributors and Translators here