Outline Overview about Web Page HTML Form Creation FORM Input INPUT control types GET & POST PHP File Upload PHP Include Files Headers Cookie Sessions.

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

Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
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.
Supplement Creating Forms. Objectives Show how forms are used How to create the Form element HTML elements used for creating input fields.
Python and Web Programming
USER INTERACTIONS: FORMS
PHP Forms. I. Using PHP with HTML Forms A very common application of PHP is to have an HTML form gather information from a website's visitor and then.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
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.
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.
PHP Forms and User Input The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
INTERNET APPLICATION DEVELOPMENT For More visit:
Forms and Form Controls Chapter What is a Form?
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Website Development with PHP and MySQL Saving Data.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
LOGO FORMs in HTML CHAPTER 5 Eastern Mediterranean University School of Computing and Technology Department of Information Technology ITEC229 Client-Side.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
CSC 2720 Building Web Applications Server-side Scripting with PHP.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
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.
+ 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.
Copyright © Texas Education Agency, All rights reserved.1 Web Technologies Website Forms / Data Acquisition.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
HTML Forms.
Week 10: HTML Forms HNDIT11062 – Web Development.
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
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.
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
8 th Semester, Batch 2008 Department of Computer Science SSUET.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
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.
1 HTML forms (cont.)
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
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.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
CHAPTER 5 SERVER SIDE SCRIPTING
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
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 ©
Introduction to Web programming
Introducing Forms.
<?php require("header.htm"); ?>
Creating Forms on a Web Page
PHP-II.
Presentation transcript:

Outline Overview about Web Page HTML Form Creation FORM Input INPUT control types GET & POST PHP File Upload PHP Include Files Headers Cookie Sessions

Overview about Web Page Most people think of a web page as nothing more than a collection of HTML code. This is fine if you happen to be a web designer. But as a PHP developer we talk about web server that generation of a document starts with an HTTP request,in which the client requests access to a resource using on method from short list methods. The client can also send data payload (called request),once request is received, the sever decoded the data that it has received and passes it on to the PHP interpreter.

Overview about Web Page A web application receives input from the user via form input Handling form input is the cornerstone of a successful web application – everything else builds on it

Overview about Web Page The browser interprets the HTML source for a particular page – Result is a combination of text, images, and entry fields – Each entry field has a specific name User fills in these fields, (with potentially some client-side input checking via JavaScript) and then selects a submission button

Overview about Web Page The browser reads the input fields, and creates a message that is sent to the server – A series of name, value pairs

HTML Form Creation FORM – Encloses all input fields – Defines where and how to submit the form data INPUT – Defines a specific input field TEXTAREA – Creates a free-form text fill-in box SELECT – Creates a menu – OPTION defines options within the menu

FORM FORM attributes – action URL of the resource that receives the filled-in form This is the URL of your PHP code that receives the input – method Choices are “get” or “post” – you should choose “post” – enctype MIME type used to send results. By default is application/xww-form-urlencoded Would use multipart/form-data if submitting a file (INPUT,type=file)

INPUT INPUT attributes – type: the kind of user input control – name: the name of the control This gets passed through to the handling code In PHP: $_POST[‘name’] – value: initial value of the control – size: initial width of the control in pixels, except for text and password controls

INPUT – maxlength: for text/password, maximum number of characters allowed – checked: for radio/checkbox, specifies that button is on – src: for image types, specifies location of image used to decorate input button

INPUT Control Types text: single input line password: single input line, with input characters obfuscated checkbox: creates a check list radio: creates a radio button list (checkbox, where inputs are mutually exclusive – only one input at a time) button: push button hidden: a hidden control. No input field is visible, but value is submitted as part of the form

INPUT Control Types Special buttons – submit: the submit button. Causes input to be sent to the server for processing – reset: the reset button. Causes all input fields to be reset to their initial values File upload – file: creates a file upload control

Example First name: Last name: Male Female

Example

Receiving form input in PHP Upon receiving a form submission, PHP automatically creates and populates two arrays with the form input data – Either : _POST[] or _GET[], depending on the FORM method type (post or get) – Additionally, _REQUEST[] is also created The array indicies are the names of the form variables (INPUT name=…) The array value is the user entry data

Receiving form input in PHP The two method allows you to send data as part of the query string, The predefined variable is used to collect values in a form ( $_GET, $_POST ).

GET Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.

GET - Example Name: Age:

<?php echo “Welcome”. $_GET["fname"].” ”; echo “You are “.$_GET["age"].” years old!”; ?>. welcome.php GET - Example

Order Form Paint Brushes Erasers Quantity: GET - Example

<?php $quantity = $_GET['quantity']; $item = $_GET['item']; echo "You ordered ". $quantity. " ". $item. ". "; echo "Thank you for ordering from Tizag Art Supplies!"; ?> process.php GET - Example

POST Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

POST - Example Name: Age:

<?php echo “Welcome”. $_POST["fname"].” ”; echo “You are “.$_POST["age"].” years old!”; ?>. welcome.php POST - Example

REQUEST The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.

REQUEST - Example <?php echo “Welcome”. $_REQUEST["fname"].” ”; echo “You are “. $_REQUEST["age"].” years old!”; ?>. welcome.php

Array Notation We can create arrays by using array notation.. <?php forech($_GET[‘arra’] as $x) { echo $x } ?>

Array Notation We can create arrays by using array notation.. <?php echo $_GET[‘arra’][‘x’]; echo $_GET[‘arra’][‘s’]; ?>

PHP File Upload To allow users to upload a file to the server, you first need to provide a form for them to specify which file they want to upload. Once they click the submit button of the form, the action page is called. This is the page that needs to contain the PHP code to process the uploaded file.

PHP File Upload Before a user can upload a file, you need to provide them with an interface that allows them to select a file and initiate the upload. The following code is an example of an input form. There are a couple of important things to note about this code: The action attribute points to a.php file. This is the file that will process the uploaded file. There is an attribute called enctype, and its value is multipart/form-data. One of the input fields has type="file".

PHP File Upload PHP File Upload Example

The Action Page Once the user uploads a file, the file is uploaded into a temporary directory on the server. If you don't move the file it will disappear. Therefore, your action page needs to move the file to another location where it can stay as long as you want it to. Whenever a file is uploaded, you can find out certain information about the file including its name, type, size, as well as the name of the temporary file on the server. These details are made available to you via a PHP array called $_FILES.

Displaying Details of the Uploaded File This code simply displays the details of the uploaded file. It doesn't move the file to another location - we'll get to that next. For now, you can use this code in conjunction with the above input form to demonstrate what happens when you upload a file to the server. Notice the PHP $_FILES array which contains info about the file. Note that we also divide the file size by 1024 in order to convert it into kb. -(Ignore any carriage returns in this example - each table row should be on one line).

Displaying Details of the Uploaded File <?php echo " "; echo " Client Filename: ". $_FILES["fileToUpload"]["name"]. " "; echo " File Type: ". $_FILES["fileToUpload"]["type"]. " "; echo " File Size: ". ($_FILES["fileToUpload"]["size"] / 1024). " Kb "; echo " Name of Temp File: ". $_FILES["fileToUpload"]["tmp_name"]. " "; echo " "; ?>

Displaying Details of the Uploaded File The above code results in something like this: Client Filename:Water lilies.jpg File Type:image/jpeg File Size: Kb Name of Temp File:C:\WINDOWS\TEMP\php48B2.tmp

Moving the Temp File As mentioned, if we want to keep the file on the server, we need to move it to another location (of our choice). The following code demonstrates how to move the file from the temporary location. move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "C:/upload/". $_FILES["fileToUpload"]["name"]);

Checking for Errors The $_FILES array includes an item for any errors that may result from the upload. This contains an error code. If there are no errors, the value is zero ( 0 ). You check this value within an "If" statement. If the value is greater than zero, you know an error has occurred and you can present a user friendly message to the user. Otherwise you can processing the file.

Checking for Errors <?php if ($_FILES["fileToUpload"]["error"] > 0) { echo "Apologies, an error has occurred."; echo "Error Code: ". $_FILES["fileToUpload"]["error"]; } else { move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "C:/upload/". $_FILES["fileToUpload"]["name"]); } ?>

Restricting File Type/Size Letting your users upload files to your server can be very risky. If you're not careful, you could get users uploading all sorts of files - perhaps including harmful executables etc. You could also find one day that you've run out of disk space because some users have been uploading enormous files. You can restrict the file types and file sizes by using an "if" statement. If the file type and size are acceptable, processing can continue, otherwise, display a message to the user.

Restricting File Type/Size Important Note: This doesn't prevent the temp file from being created. The file needs uploaded to the server before PHP can find out the file size and type. This simply prevents the file from being moved to your "permanent" location - hence the file should disappear and (hopefully) not become a problem. In any case, I recommend that you install good anti-virus software before allowing users to upload files to your server.

Restricting File Type/Size <?php if (($_FILES["fileToUpload"]["type"] == "image/gif") || ($_FILES["fileToUpload"]["type"] == "image/jpeg") || ($_FILES["fileToUpload"]["type"] == "image/png" ) && ($_FILES["fileToUpload"]["size"] < 10000)) { move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "C:/upload/". $_FILES["fileToUpload"]["name"]); } else { echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb"; } ?>

PHP Include Files In PHP, you can insert the content of one PHP file into another PHP file before the server executes it. The include and require statements are used to insert useful codes written in other files, in the flow of execution. Include and require are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script include will only produce a warning (E_WARNING) and the script will continue

PHP Include Files Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file. include 'filename'; or require 'filename';

PHP Include Files Welcome to my home page! Some text.

PHP Include Files Assume we have a standard menu file that should be used on all pages. "menu.php": <?php echo ' Home Tutorials References Examples About Us Contact Us '; ?>

PHP Include Files All pages in the Web site should include this menu file. Here is how it can be done: Welcome to my home page. Some text.

Header The header() function sends a raw HTTP header to a client. It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem):

Header The header() function sends a raw HTTP header to a client. It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem):

Header header(string,replace,http_response_code) ParameterDescription stringRequired. Specifies the header string to send replaceOptional. Indicates whether the header should replace previous or add a second header. Default is TRUE (will replace). FALSE (allows multiple headers of the same type) http_response _code Optional. Forces the HTTP response code to the specified value (available in PHP 4.3 and higher)

Header <?php // This results in an error. // The output above is before the header() call header('Location: //this is redirect to this website. ?>

Cookie A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

How to Create a Cookie? The setcookie() function is used to set a cookie. Note: The setcookie() function must appear BEFORE the tag. setcookie(name, value, expire, path, domain);

How to Create a Cookie? We will create a cookie named "user" and assign the value "Ali" to it. We also specify that the cookie should expire after one hour:

Cookie Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).

How to Create a Cookie? You can also set the expiration time of the cookie in another way. It may be easier than using seconds.

How to Retrieve a Cookie Value? The PHP $_COOKIE variable is used to retrieve a cookie value. In the example below, we retrieve the value of the cookie named "user" and display it on a page: <?php echo $_COOKIE["user"]; // Print a cookie print_r($_COOKIE); // A way to view all cookies ?>

How to Delete a Cookie? When deleting a cookie you should assure that the expiration date is in the past.

Session PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application. Before you can store user information in your PHP session, you must first start up the session.

Starting a PHP Session The code above will register the user's session with the server, allow you to start saving user information. The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:

Storing a Session Variable <?php session_start(); // store session data $_SESSION['views']=1; echo "Pageviews=". $_SESSION['views']; ?>

Destroying a Session if you wish to delete some session data, you can use the unset() or the session_destroy() function. The unset() function is used to free the specified session variable:

Session Or <?php session_destroy(); // delete all sessions ?>