CSE 154 LECTURE 18: FORMS AND UPLOADING FILES. Exercise: Baby name web service JSON Modify our babynames.php service to produce its output as JSON. For.

Slides:



Advertisements
Similar presentations
HTML Forms. collect information for passing to server- side processes built up from standard widgets –text-input, radio buttons, check boxes, option lists,
Advertisements

23-Aug-14 HTML/XHTML Forms. 2 What are forms? is just another kind of XHTML/HTML tag Forms are used to create (rather primitive) GUIs on Web pages Usually.
24-Aug-14 HTML Forms. 2 What are forms? is just another kind of HTML tag HTML forms are used to create (rather primitive) GUIs on Web pages Usually the.
Ruby (on Rails) CSE 190M, Spring 2009 Week 3. Web Programming in Ruby Ruby can be used to write dynamic web pages Similar to PHP, chunks of Ruby begins.
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.
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.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
More forms CS Reset Buttons  specify custom text on the button by setting its value attribute CS380 2 Name: Food: Meat? HTML.
Chapter 6 Basic forms 1. Forms and query string 2 form : a group of user input (UI) controls that accepts information from the user and sends the information.
Video, audio, embed, iframe, HTML Form
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
Python and Web Programming
Using Entities & Creating Forms Jill R. Sommer Institute for Applied Linguistics Kent State University.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
FILE UPLOADS CHAPTER 11. THE BASIC PROCESS 1.The HTML form displays the control to locate and upload a file 2.Upon form submission, the server first stores.
Uploading Files. Why? By giving a user the option to upload a file you are creating an interactive page You can enable users have a greater web experience.
CSE 154 LECTURE 9: FORMS. Web data most interesting web pages revolve around data examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes can.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
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.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
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.
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:
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Server-side Scripting Powering the webs favourite services.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Website Development with PHP and MySQL Saving Data.
LOGO FORMs in HTML CHAPTER 5 Eastern Mediterranean University School of Computing and Technology Department of Information Technology ITEC229 Client-Side.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
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,
Creating PHPs to Insert, Update, and Delete Data CS 320.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Forms Collecting Data CSS Class 5. Forms Create a form Add text box Add labels Add check boxes and radio buttons Build a drop-down list Group drop-down.
Web Technologies Lecture 3 Web forms. HTML5 forms A component of a webpage that has form controls – Text fields – Buttons – Checkboxes – Range controls.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
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.
AJAX. Ajax  $.get  $.post  $.getJSON  $.ajax  json and xml  Looping over data results, success and error callbacks.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
HTML FORM AND PHP IST 210: Organization of Data IST210 1.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CSE 154 LECTURE 17: WEB SERVICES. What is a web service? web service: software functionality that can be invoked through the internet using common protocols.
CGS 3066: Web Programming and Design Spring 2016 PHP.
Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
Introduction to Web Site Development Department of Computer Science California State University, Los Angeles Lecture 8: Forms and Controls.
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.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Radoslav Georgiev Telerik Corporation
Unit 7 How to Upload Files. A very useful aspect of PHP is its ability to manage file uploads to your server. Before you can use PHP to manage your uploads,
CSE 154 LECTURE 18: WEB SERVICES. Exercise: Baby name web service Write a web service that accepts a name and gender and finds and outputs the line from.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
How to Write Web Forms By Mimi Opkins.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
HTML Forms and Server-Side Data
DBW - PHP DBW2017.
Super Global Arrays Forms and PHP "Superglobal" arrays (6.4.1)
CSE 154 Lecture 24: JSON.
Lecture 19: Forms and uploading files
CMPT241 Web Programming Chapter 6 Forms.
PHP-II.
Web Forms.
Presentation transcript:

CSE 154 LECTURE 18: FORMS AND UPLOADING FILES

Exercise: Baby name web service JSON Modify our babynames.php service to produce its output as JSON. For the data: Morgan m The service should output the following JSON: { "name": "Morgan", "gender": "m", "rankings": [375, 410, 392, 478, 579, 507, 636, 499, 446, 291, 278, 332, 518] } JSON

Emitting JSON data manually... header("Content-type: application/json"); print "{\n"; print " \"books\": [\n"; foreach ($books as $book) { print " {\"author\": \"{$book['author']}\", \"title\": \"{$book['title']}\"}\n"; } print "\n"; specify a content type of application/json messy, just like when manually printing XML (not recommended)

PHP's JSON functions PHP includes the following global functions for interacting with JSON data: json_decode( string )parses the given JSON data string and returns an equivalent associative array object (like JSON.parse in JavaScript) json_encode( object )returns JSON equivalent for the given object or array or value (like JSON.stringify in JavaScript) json_encode will output associative arrays as objects and normal arrays as arrays

PHP JSON example <?php $data = array( "library" => "Odegaard", "category" => "fantasy", "year" => 2012, "books" => array( array("title" => "Harry Potter", "author" => "J.K. Rowling"), array("title" => "The Hobbit", "author" => "J.R.R. Tolkien"), array("title" => "Game of Thrones", "author" => "George R. R. Martin"), array("title" => "Dragons of Krynn", "author" => "Margaret Weis"), ) ); header("Content-type: application/json"); print json_encode($data); ?> PHP

PHP JSON example - output { "library": "Odegaard", "category": "fantasy", "year": 2012, "books": [ {"title": "Harry Potter", "author": "J.K. Rowling"}, {"title": "The Hobbit", "author": "J.R.R. Tolkien"}, {"title": "Game of Thrones", "author": "George R. R. Martin"}, {"title": "Dragons of Krynn", "author": "Margaret Weis"}, ] } JSON

HTML forms form: a group of UI controls that accepts information from the user and sends the information to a web server the information is sent to the server as a query string JavaScript can be used to create interactive controls (seen later)

HTML form: form controls HTML required action attribute gives the URL of the page that will process this form's data when form has been filled out and submitted, its data will be sent to the action's URL one page may contain many forms if so desired

Form example Let's search Google: HTML Let's search Google: output must wrap the form's controls in a block element such as div

Reset buttons Name: Food: Meat? HTML output when clicked, returns all form controls to their initial values specify custom text on the button by setting its value attribute

Hidden input parameters Name SID HTML an invisible parameter that is still passed to the server when form is submitted useful for passing on additional state that isn't modified by the user output

HTTP GET vs. POST requests GET : asks a server for a page or data if the request has parameters, they are sent in the URL as a query string POST : submits data to a web server and retrieves the server's response if the request has parameters, they are embedded in the request's HTTP packet, not the URL For submitting data to be saved, POST is more appropriate than GET GET requests embed their parameters in their URLs URLs are limited in length (~ 1024 characters) URLs cannot contain special characters without encoding private data in a URL can be seen or modified by users

Form POST example Name: Food: Meat? HTML output

The htmlspecialchars function text from files / user input / query params might contain, &, etc. we could manually write code to strip out these characters better idea: allow them, but escape them htmlspecialcharsreturns an HTML-escaped version of a string $text = " hi 2 u & me "; $text = htmlspecialchars($text); # "<p>hi 2 u & me</p>"

GET or POST? if ($_SERVER["REQUEST_METHOD"] == "GET") { # process a GET request... } elseif ($_SERVER["REQUEST_METHOD"] == "POST") { # process a POST request... } PHP some PHP pages process both GET and POST requests to find out which kind of request we are currently processing, look at the global $_SERVER array's "REQUEST_METHOD" element

A form that submits to itself... HTML a form can submit its data back to itself by setting the action to be blank (or to the page's own URL) benefits fewer pages/files (don't need a separate file for the code to process the form data) can more easily re-display the form if there are any errors

Processing a self-submitted form if ($_SERVER["REQUEST_METHOD"] == "GET") { # normal GET request; display self-submitting form ?>... <?php } elseif ($_SERVER["REQUEST_METHOD"] == "POST") { # POST request; user is submitting form back to here; process it $var1 = $_POST["param1"];... } PHP a page with a self-submitting form can process both GET and POST requests look at the global $_SERVER array to see which request you're handling handle a GET by showing the form; handle a POST by processing the submitted form data

Uploading files <form action=" method="post" enctype="multipart/form-data"> Upload an image as your avatar: HTML add a file upload to your form as an input tag with type of file must also set the enctype attribute of the form output

Processing an uploaded file in PHP uploaded files are placed into global array $_FILES, not $_POST each element of $_FILES is itself an associative array, containing: name : the local filename that the user uploaded type : the MIME type of data that was uploaded, such as image/jpeg size : file's size in bytes tmp_name : a filename where PHP has temporarily saved the uploaded file to permanently store the file, move it from this location into some other file

Uploading details HTML example: if you upload borat.jpg as a parameter named avatar, $_FILES["avatar"]["name"] will be "borat.jpg" $_FILES["avatar"]["type"] will be "image/jpeg" $_FILES["avatar"]["tmp_name"] will be something like "/var/tmp/phpZtR4TI" output

Processing uploaded file, example $username = $_POST["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"; } PHP 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