Forms and PHP

Forms and PHP

FORMS AND PHP Super Global Arrays Array $_REQUEST $_GET, $_POST $_FILES $_SESSION, $_COOKIE Description parameters passed to any type of request parameters passed to GET and POST requests files uploaded with the web request used to identify the user, maintain sessions Forms Submit Data

Guessing game...

http://www.php-intro.com/code/forms/form1.ph Forms Submit Data form1.php

Guessing game...

$_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

Guessing game...

form2.php

 $_GET:  

Guessing game...

form3.php

 $_POST:  $_GET:  

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 Web Server HTTP Request Browser 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 HTML forms enable your web application to collect information from your users Browse r Type URL Show form User fills out form Web server Server-side Programs

Gimme HTML HTML for form Do something with Send values enteredthese values, please If two people GET the same URL, the proxy server can GET the URL once and give the data to both. Browser Programs Browser Programs Proxy Serv ers Web server Programs

Databas e SMTP server 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) Persisting

Guessing game...

/>

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

DIE DIE" />

form4.php To The Rescue: htmlentities()

/>

form5.php

/>

Processing POST Data There are many patterns for handling POST data No "rules" just "suggestions" What about frameworks?

$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...

$message

\n");

} ?>

/>

Completely process incoming POST data (if any) produce no output. Produce the page output. guess.php

A Guessing game

Guessing game...

if ( $message !== false ) { echo("

$message

\n"); } ?>

/>

guess.php

if ( $guess == 42 ) { $message = "Great job!"; } else if ( $guess < 42 ) { $message = "Too low"; } else { $message = "Too high..."; } } ?> A Guessing game

Guessing game...

$message

\n"); } ?>

/>

?> ... guess.php

A Guessing game

Guessing game...

$message

\n"); } ?>

/>

... ?>

A Guessing game

Guessing game...

$message

\n"); } ?>

/>

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

$message

\n"); } ?>

/>

guess.php Other Input Types Text

Password Radio Button Check Box Select / Drop-Down TextArea tp://www.php-intro.com/code/forms/more.php

Many field types...

more.php $_POST: Array ( [account] => Beth [pw] => 12345 [nick] => BK [when] => pm ... )

more.php

Preferred Time:
AM
PM

$_POST: Array( ... [nick] => BK [when] => pm [class] => si502 ... )

Classes taken:
SI502 - Networked Tech
SI539 - App Engine
SI543 - Java

$_POST: Array( ... [when] => pm [class1] => si502 [soda] => 0 ... ) more.php $_POST: Array( ... [when] => pm [class3] => on [soda] => 0 ... )

The values can be any string but numbers are used quite often. more.php $_POST: Array( ... [class] => si502 [soda] => 0 [snack] => peanuts ... )

more.php $_POST: Array( ... [class] => si502 [soda] => 0 [snack] => peanuts ... ) more.php

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

On submit input types, the text is both in the UI and in $_POST. $_POST: Array( ... [dopost] => Submit ... ) 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 Select your favorite color:
Birthday:
E-mail:
Quantity (between 1 and 5):
Add your homepage:
Transportation:
Validation happens when you press submit. Upload an image as your avatar:

Uploading File

Upload an image as your avatar:

Upload an image as your avatar:Choose File No file chosen Submit Upload an image as your avatar: Uploading File uploaded files are placed into global array $_FILES, not $_REQUEST

each element of $_FILES is itself an associative array, containing: name type size tmp_name : a filename where PHP has temporarily saved the uploaded file : the local filename that the user uploaded : the MIME type of data that was uploaded, such as image/jpeg : file's size in bytes

to permanently store the file, move it from this location into some other file Upload an image as your avatar: Uploading File $username = $_REQUEST["username"]; if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) { move_uploaded_file($_FILES["avatar"]["tmp_name"], "$username/avatar.jpg"); print "Saved uploaded file as $username/avatar.jpg\n"; } else { print "Error: required file not uploaded"; } functions for dealing with uploaded files:

is_uploaded_file(filename) returns TRUE if the given filename was uploaded by the user move_uploaded_file(from, to) moves from a temporary file location to a more permanent file proper idiom: check is_uploaded_file, then do move_uploaded_file Upload an image as your avatar: Uploading File

Upload an image as your avatar:

Upload an image as your avatar:Choose File No file chosen Submit

Recently Viewed Presentations

  • _x0016_

    _x0016_

    ArqRealitat, representació de la realitat, recreació sobre la representació de la realitatLLOC-TOPOS Catalina Salvà La creació del lloc, emplaçar-se, situar-se, desvetllar les qualitats del lloc.PROGRAMA I PROJECTE Franc Fernandez Atributs i qualitats del què és i vol ser l'espai: dispositius...
  • How to Round and Estimate

    How to Round and Estimate

    Calibri MS Pゴシック Arial Bookman Old Style Wingdings Office Theme How to Round and Estimate What are the rules for rounding? PowerPoint Presentation "Look next door" More rounding to the nearest ten More rounding to the nearest ten Are you...
  • Folie 1 - Startseite

    Folie 1 - Startseite

    Das „Wetter" auf unserer Sonne und die Auswirkungen auf die Erde Abschlusspräsentation am 31.01.2012 Maria Heinrich Q12 die Magnetosphäre wird auf der Tagseite bis zur Hälfte ihrer normalen Größe komprimiert das Polarlichtoval kann sich bis zum Äquator ausdehnen die dabei...
  • Challenges Presented by Organizational Identifiers

    Challenges Presented by Organizational Identifiers

    *following up on observations made by Kenning Arlitsch, Google Knowledge cards for libraries vary in quality. LC looks good, but KC for the London (Ohio) public library is just a location on a map. Kenning's argues that the KC can...
  • Business Rental Presentation Erin Kramarich Director of Business

    Business Rental Presentation Erin Kramarich Director of Business

    Early Start. Deliveries for Early Monday Morning. Delivered on Friday afternoon between 4-6 PM. $10 per day charge ($20 Max) for the weekend. If travel is commencing earlier (Sunday for example)
  • The Human Body - MARRIC

    The Human Body - MARRIC

    The Human Body Did you Know??!! When you sneeze all body functions stop, even your heart Your brain is 80% water A human has 60,000 miles of blood vessels in their body The lining of your digestive system is shed...
  • Administering HPV vaccine

    Administering HPV vaccine

    Health worker verifies girl's vaccination card and documentsthe following information in the vaccination register and on the vaccination card if available: Name of girl, Address, Date of birth and age (if unknown, determine age based on educated guess and document),...
  • Mechanical Vibrations - DEU

    Mechanical Vibrations - DEU

    Equation of motion (Governing equation) b) By Newton's Second Law System FBD Free-Body Diagram (FBD) Example: Find equation of motion of the following system by using Newton's Second Law. Example: Find equation of motion of the following system by using...