Forms and PHP Dr. Charles Severance

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,
Introduction to PHP Dr. Charles Severance
Tutorial 6 Creating a Web Form
Lecture 14 HTML Forms CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
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.
Using Entities & Creating Forms Jill R. Sommer Institute for Applied Linguistics Kent State University.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
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.
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.
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
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.
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,
HTML Form and PHP IST Review of Previous Class HTML table and PHP array Winner is chosen randomly using rand() function.
CHAPTER 7 Form & PHP. Introduction All of the following examples in this section will require two web pages. The first page retrieves information posted.
HTTP - Forms - AppEngine Jim Eng
Dynamic web content HTTP and HTML: Berners-Lee’s Basics.
PHP Arrays Dr. Charles Severance
1 CSC317/318 INTERNET PROGRAMING / DYNAMIC WEB APPLICATION DEVELOPMENT Siti Nurbaya Ismail Faculty of Computer & Mathematical Sciences, Universiti Teknologi.
Google Application Engine Introduction Jim Eng with thanks to Charles Severance
Introduction to Dynamic Web Content Dr. Charles Severance
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
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.
PHP Arrays Dr. Charles Severance
Tutorial 6 Creating a Web Form
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
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 – User Input / Action Guessing game... Input Guess

Web ServerDatabase Server Time Apache PHP MySql Browser JavaScri pt DOMDOM ind.php Parse Respons e Parse Reques t $_GET

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

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 /form1.php?guess=42 Accept: text/html User-Agent: Lynx/2.4 libwww/2.14 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 HTTP Request Browser Web Server

Web ServerDatabase Server Time Apache PHP MySql Browser JavaScri pt DOMDOM ind.php Parse Respons e Parse Reques t $_POST

Rules of the POST/GET Choice 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" value=" "/> form4.php Review: Ternary Operation Persisting Form Data Across Requests

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

Input Guess <input type="text" name="guess" id="guess size="40" "value=""> DIE DIE " /> form4.php

To The Rescue: htmlentities() Input Guess <input type="text" name="guess" id="guess" size="40" value=" "/> form5.php

Input Guess <input type="text" name="guess" id="guess" size="40" value=" "/> <input type="text" name="guess" id="guess" value=""><b>DIE DIE</b>" />

Pattern: 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 data (if any) - produce no output. Produce the page output. guess.php What about frameworks?

Model-View-Controller A model that defines the elements of a web application and how they interact. View – Produce output Model – Handle data Controller – Orchestration / Routing

guess.php <?php $oldguess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $oldguess = $_POST['guess'] + 0; if ( $oldguess == 42 ) { $message = "Great job!"; } else if ( $oldguess < 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" value=" Model View Controller Context

guess.php No HTML No Database Controller Context <?php $oldguess = ''; $message = false; if ( isset($_POST['guess']) ) { // Trick for integer / numeric parameters $oldguess = $_POST['guess'] + 0; if ( $oldguess == 42 ) { $message = "Great job!"; } else if ( $oldguess < 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" value="

<?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" value=" <?php $oldguess = ''; $message = false; if ( isset($_POST['guess']) ) { // Nifty trick $oldguess = $_POST['guess'] + 0; if ( $oldguess == 42 ) { $message = "Great job!"; } else if ( $oldguess < 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" value="... ?> A Guessing game Guessing game... <?php if ( $message !== false ) { echo(" $message \n"); } ?> Input Guess <input type="text" name="guess" id="guess" size="40" value="

<?php $oldguess = ''; $message = false; if ( isset($_POST['guess']) ) { // Nifty trick $oldguess = $_POST['guess'] + 0; if ( $oldguess == 42 ) { $message = "Great job!"; } else if ( $oldguess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } ?>... guess.php Note: This code is a little sloppy in terms of its data validation

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

Web ServerDatabase Server Time Apache PHP MySql Browser JavaScri pt DOMDOM ind.php Parse Respons e Parse Reques t

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