Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
JavaScript Forms Form Validation Cookies CGI Programs.
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
Session Management A290/A590, Fall /25/2014.
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.
Tutorial 6 Forms Section A - Working with Forms in JavaScript.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
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.
CST JavaScript Validating Form Data with JavaScript.
PHP: Introduction By Trevor Adams.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
Client-Side programming with JavaScript 3
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.
ITM © Port, Kazman1 ITM 352 HTML Forms, Basic Form Processing.
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.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
PHP : Hypertext Preprocessor
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
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 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.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
JavaScript, Fourth Edition
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Chapter 8 Cookies And Security JavaScript, Third Edition.
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.
HTML Forms.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
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,
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.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Web Technologies Lecture 3 Web forms. HTML5 forms A component of a webpage that has form controls – Text fields – Buttons – Checkboxes – Range controls.
©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.
Netprog CGI and Forms1 CGI and Forms A detailed look at HTML forms.
HTML Forms.
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.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
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.
COSC 2328 – Web Programming.  PHP is a server scripting language  It’s widely-used and free  It’s an alternative to Microsoft’s ASP and Ruby  PHP.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CIS 228 The Internet 12/6/11 Forms and Validation.
INTERNET APPLICATIONS CPIT405 Forms, Internal links, meta tags, search engine friendly websites.
CGS 3066: Web Programming and Design Spring 2016 PHP.
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.
Session 2 Basics of PHP.
How to Write Web Forms By Mimi Opkins.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
PHP: Security issues FdSc Module 109 Server side scripting and
Forms, cont’d.
PHP Forms and Databases.
PHP-II.
Presentation transcript:

Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end database etc  Large amounts of data can pass from the user to the server (parameters can be passed)  Used in areas like e-commerce

Forms – getting the input Use a normal XHTML form to get user input, e.g. … Surname: Address: …

Forms – getting the Input Different form elements can be used to collect input, e.g.   Also radio buttons, checkboxes, select lists, and hidden Action –  Appends data onto end of http request information  Specifies the PHP script stored on the server that we want to send the data to (e.g. processData.php), so that the script can get it and process it Submit  User completes the form and the request for the processing script is sent to the server when the “submit” button is clicked  The script will then run on the server

Forms 1 - requesting the form Client requests the XHTML web form from the server, the server then sends the XHTML web form to the client browser where it is displayed Browser Client (e.g. IE) Response (the text/XHTML of the web form) Web Server Software e.g. Apache / IIS Files in local_html: getDataFrm.html processData.php Web Server Request for getDataFrm.html

Forms 2 - sending data / the response Data entered on the form is sent by the browser (in parameter name-value pairs) along with the request to the server for the PHP script when the form submit button is clicked Browser Client (e.g. IE) Request for processData.php (with parameter name-value pairs e.g. surname – elvin address - SCEIS) Response (text/XHTML) Web Server Software e.g. Apache / IIS Files in local_html: getDataFrm.html processData.php PHP Processor Web Server

Parameter name-value pairs Form data is sent in name-value pairs  e.g. surname-bloggs, game-chess name – the name of the form component specified by the name part of the tag, e.g. … Chess Drafts … value – this is the value entered or selected by the user for the particular form component

GET and POST HTTP governs how web browsers request files from web servers and how servers send files back There are two HTTP methods to pass parameters to the server (to be processed by scripts): GET  GET requests encode form parameters in the URI (in a query string)  i.e. they append data to the URI, e.g. appendExample.php?surname=kasparov, game=chess POST  For posting lots of data to the server  Sends data within the body of an HTTP request  Not sent via the URI, therefore invisible in the browser It is possible to use either with XHTML web forms

Getting and processing data The $_POST and $_GET arrays can be used by a PHP script to access form parameters The keys are the parameter names Example: the PHP code for the processData.php script <?php $surname = $_POST[‘surname’]; $game = $_POST[‘game’]; echo “Surname: $surname "; echo “Game: $game”; ?> Here the parameter values from POST for “surname” and “game” are first copied into variables, and then sent back to the browser in the response for display The response will vary depending on what the user entered You need to use $_POST (or $_GET) for every INPUT field in the web form

Forms: an overview Remember  Forms allow users to input data  Parameter name-value pairs are passed with the request to the server  This data can be processed by the script e.g. get it using $_POST (or $_GET) and store it in variables  The data can be processed in any way that you like, including display back to the browser (using echo) via the response  The data sent to the form can vary, and therefore so can the response The particular response is not saved in a file, it is only seen by the browser that requested the script from the server

Using GET: example Data parameters encoded in the URI (in a Query String) Example: Product 75 Processing PHP script (viewp.php): $prodID = $_GET['productID']; echo "Product Id: $prodID - "; if ($prodID == "55") echo "Blue shirt with polka dots"; if ($prodID == "75") echo "Batman outfit"; Query String

On condition … Conditional statements allow decision making depending on conditions  Allows programs to be dynamic, executing different pieces of code depending on a condition Conditional statements include  if/else  switch

if statement: making decisions Use: evaluate truth of expression (condition). Format of if if (expression) statement else alternative statement if the expression was false elseif format: if (expression 1) statement 1 elseif (expression 2) statement 2 else default statement if both expressions were false What operators are there?  e.g. !=,, >=, <=, ==, &&, AND, ||, OR

Example web site Requests and passes parameters to loginEX.php

Code for loginEx.php … $userN = $_POST[‘username’]; $pass = $_POST[‘password’]; echo "Hello $userN "; if ($pass == “adminpass") { echo " Enter secure Menu area"; } elseif ($pass == "bob") { echo “Successful login Enter site"; } else { echo "Password invalid. Try again "; } …

Some string functions strlen(x)- no of characters in a string strpos(x, “a”)- returns the position, e.g. of a substr(x, start [, length])- copies piece of string substr_replace(x, y, start[, length])- replaces x with y strrev(x)- reverses the string trim(x)- removes white space ltrim(x)- removes white space from start of string rtrim(x)- removes white space from end of string strtolower(x)- to lower case strtoupper(x)- to upper case ucfirst(x)- makes first character upper case ucwords(x)- first character of each word to uppercase

Improving the login system Validating input How could we:  Check if the user name was of the correct length?  Remove white space?  Remove the effect of case?

Code for loginEx2.php … $userN = trim($_POST['username']); if (strlen($userN) !=5) echo "User names must be exactly 5 characters long. Please try again"; else { $pass = strtolower(trim($_POST['password'])); echo "Hello $userN "; if ($pass == "adminpass") { echo " Enter secure Menu area"; } elseif ($pass == "bob") { echo "Successful login Enter site"; } else { echo "Password invalid. Try again "; } } …

Further validation Requests and passes parameters to changePassEx.php How could we: Check that anything had been entered? Check whether the two entries match?

Code for changePassEx.php Another example using control structures and string functions if ((strlen($_POST['newP1']) == 0) && (strlen($_POST['newP2']) == 0)) { echo "You have not entered a new password. Try again"; } elseif ((strlen($_POST['newP1']) == 0) || (strlen($_POST['newP2']) == 0)) { echo "You haven't entered the new password twice"; } elseif (strcmp($_POST['newP1'], $_POST['newP2']) != 0) { echo "The passwords entered are not the same. Please try again. "; } else { echo "Thanks for the new password"; } ?>

switch statement An alternative way of making conditional decisions Unlike with if /elseif, switch evaluates only one expression Used when a single value determines a choice (and also used when if’s start to look confusing) switch (expression) case value1: // code to execute if the expression evaluates to value1 break; case value2: // code to execute if the expression evaluates to value2 break; case value3: // code to execute if the expression evaluates to value3 break; default: // code to execute if none of the cases above are true

switch example: custMenu.php … Some code to display a menu, then Current Special Offers <?php $month = date("n"); switch ($month) { case 2: echo "Love song album deals for Valentines day"; break; case 12: echo "Festive season favourites at half price"; break; default: echo "A general special offer"; } ?> …