Simple PHP Web Applications Server Environment <?php... ?> <?php... ?>

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
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.
Lecture 14 HTML Forms CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
A simple PHP application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the application returns.
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.
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
USER INTERACTIONS: FORMS
Simple PHP application. A simple application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the.
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.
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.
Unit 7 – Working with Forms 1. Creating a form 2. Accessing the submitted data 3. Common operations on forms.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
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.
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.
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:
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
DAT602 Database Application Development Lecture 14 HTML.
1 Forms A form is the usual way that information is gotten from a browser to a server –HTML has tags to create a collection of objects that implement this.
Week 7. Lecture 3 PHP Forms. PHP forms In part 2 of this course, we discussed html forms, php form is similar. Lets do a quick recap of the things we.
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.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
Website Development with PHP and MySQL Saving Data.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
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.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Session 8: Working with Form iNET Academy Open Source Web Development.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Part 2 Lecture 9 PHP Superglobals and Form Handling.
©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.
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.
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.
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.
Ashima Wadhwa Java Script And Forms. Introduction Forms: –One of the most common Web page elements used with JavaScript –Typical forms you may encounter.
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
In this session, you will learn to:
ITM 352 HTML Forms, Basic Form Processing
How to Write Web Forms By Mimi Opkins.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
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 ©
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
Web Programming– UFCFB Lecture 17
Intro to PHP & Variables
Validation and Building Small Apps
Cookies BIS1523 – Lecture 23.
Simple PHP application
Introducing Forms.
Forms, cont’d.
Web DB Programming: PHP
Creating Forms on a Web Page
Lecture 5: Functions and Parameters
The HTTP Protocol COSC 2206 Internet Tools The HTTP Protocol
PHP Forms and Databases.
PHP-II.
Presentation transcript:

Simple PHP Web Applications Server Environment <?php... ?> <?php... ?>

Simple PHP WEB applications Hello world and date scripts PHP info The server environment External global variables Form processing Page counts

7/9/20163 Hello Script (HTML Format) Hello Script Hello PHP World! "; ?>

7/9/20164 Hello Script (Plain Format) Hello PHP World! "; ?> ?> default type is text/html

7/9/20165 Date Script Date Script Displaying the date and time see PHP docs for more formats

7/9/20166 Display PHP Info PHP Info

7/9/20167 Server Environment A PHP script is provided with a superglobal associative array called $_SERVER of variables that describe the server environment: Example: $_SERVER['DOCUMENT_ROOT'] this is a string giving the document root directory, for example c:\Apache\htdocs

7/9/20168 Global Environment $GLOBALS is a superglobal associative array whose keys are the names of the global arrays that are available in PHP. The corresponding values are these arrays Example: $GLOBALS['_SERVER'] This is the $_SERVER array You probably won't use $GLOBALS much

7/9/20169 Display $_SERVER Variables Write a simple script that displays the $_SERVER array as an HTML table: Write a function to display an associative array as an HTML table Write a PHP script server.php that uses it to make an HTML page containing the array.

7/9/ server.php (1) A function to display an associative array: put it in file display_table.php ', "\n"; foreach ($a as $key => $value) { echo " \n"; echo " $key \n $value \n"; echo " \n"; } echo " \n"; } ?>

7/9/ Partial output of server.php Much More

7/9/ Display $GLOBALS Variables Write a simple script that displays the associative arrays in $GLOBALS as HTML tables use the display_tables function in a script called globals.php

7/9/ globals.php (1) Values in $GLOBALS array Values in $GLOBALS array

7/9/ globals.php (2) foreach ($GLOBALS as $key => $value) { echo " $key "; if (! empty($value)) { display_table($value); } else { echo "$key is empty"; } }

7/9/ Partial Output of globals.php More

7/9/ The GET Method A script can be invoked using a query string of encoded name-value pairs This can be done explicitly by including a query string at the end of the URL Example: ?name1=val1&name2=val2.... Alternatively the query string can be provided by a form using the GET method

7/9/ The Query String Try the following link Look for the QUERY_STRING table row This shows that the query string is made available to the PHP script as $_SERVER['QUERY_STRING']

7/9/ $_GET and $_REQUEST arrays The name value pairs are made available to the script using the global $_ GET or $_ REQUEST arrays. Example: if one of the name value pairs is name=Fred then $_ REQUEST['name'] and $_ GET['name'] both have the value 'fred’

7/9/ THE POST method There are limitations to the GET method: long strings may exceed browser limitations Name value pairs are visible to anyone However, URL's can be bookmarked POST method doesn't have these limitations. Forms can specify either GET or POST In either case the name value pairs are always available in the $_REQUEST array

7/9/ GET and POST summary PHP scripts interact with the client using the GET and POST methods and the superglobal associative arrays: $_GET contains any name-value pairs sent using the GET method. $_POST contains any name-value pairs sent using the POST method. $_REQUEST contains all name-value pairs. $_COOKIE contains cookie name-value pairs

7/9/ Determining the method A script can determine whether it was invoked using the GET or PUT method Use $_SERVER['REQUEST_METHOD'] It will have the value 'GET' or 'PUT' so a test can be made if ($_SERVER['REQUEST_METHOD'] === 'GET') {... }

7/9/ Get method example The GET method The GET method "; if ($request === 'GET') { echo "The query string is $query "; } ?>

7/9/ Form processing Browsers communicate with server-side scripts using HTML forms When you fill out a form you are specifying a number of name-value pairs. When you submit the form the name-value pairs are sent to the server-side script In the case of PHP these values are available to the script in the arrays $_GET, $_POST, $_REQUEST

7/9/ Older versions of PHP Older versions of PHP (before 4.1) directly assigned form variable names to PHP variables of the same name This could be exploited (security hole) Now we should always use the superglobal arrays like $_GET, $POST, $_REQUEST This is the default with register_globals turned off in php.ini (this is now the default)

7/9/ Form processing using GET The form has the following structure When the form is submitted the script called doform.php will be called using the GET method......

7/9/ Link processing using GET A link of the form.. can be processed using the GET method and the name value-pairs can be accessed using $_GET['name'] which has the value 'fred' $_GET['age'] which has the value 24

7/9/ Form processing using POST The form has the following structure When it is submitted the script called doform.php will be called using the POST method......

7/9/ Input field example The value is omitted if there is no default value When the form containing this element is submitted the value is available as $_REQUEST['firstname']

7/9/ form1_get.html Processing form with GET Processing form with GET First name: Last name:

7/9/ form1_post.html Processing form with POST Processing form with POST First name: Last name:

7/9/ doform1.php Form Results Form Results view source (forms/doform1.php)

7/9/ Try it directly with a URL The doform1.php script can be requested using a URL instead of a form For example, try name=Fred&lastname=Jones name=Fred&lastname=Jones

7/9/ Form Generation with PHP In this example the form is generated by a static HTML page. A PHP script can display both the HTML page containing the form and the HTML page in resonse to the form submission It knows which page to display by detecting whether the submit button has been clicked or not:

7/9/ Form processing logic If the input element for the button is then the PHP script logic has the form if (isset($_REQUEST['button'])) { display_output_page(); } else { display_form_page(); }

7/9/ display_form_page() Forms, version 2 Forms, version 2 " method="POST"> First name: Last name:

7/9/ doform2.php <?php if (isset($_REQUEST['button'])) { display_output_page(); } else { display_form_page(); } ?> view source forms/doform2.php

7/9/ Using PHP_SELF Locate the script using $self = $_SERVER['PHP_SELF']; This gives the script location relative to the document root. For example /php/forms/doform2.php To see this view the html before submitting the form.

7/9/ Using Multiple Buttons (1) Give the buttons different names: Check which one was clicked if ( isset($_REQUEST['button1']) || isset($_REQUEST['button2']) { display_output_page(); } else { display_form_page(); }

7/9/ Using Multiple Buttons (2) $button1 = isset($_REQUEST['button1'] ? $_REQUEST['button1'] : ''; $button2 = isset($_REQUEST['button2'] ? $_REQUEST['button2'] : ''; if ($button1 != '') { // process button1 click event here } if ($button2 != '') { // process button2 click event here } view script forms/doform3.php

7/9/ Error Checking Names contain only letters, hyphens, and '

7/9/ Sticky input fields If there are errors in a form it needs to be sent to the user to be corrected so we can supply the values that the user entered as defaults. First name: "> Last name: "> view script forms/doform4.php

7/9/ Checkbox group (1) Use array notation [] in the form to indicate a group of checkboxes Which wines do you like? Shiraz Merlot Chianti

7/9/ Checkbox group (2) Determining which boxes are checked if (! empty($wines)) { echo "Wines selected were "; foreach ($wines as $wine) { echo "$wine "; } } else echo "No wines were selected"; view script forms/doform5.php

7/9/ Drop down list Use select and option. The selected attribute indicates which choice has been made. If no choice is made the first one is selected Select your favourite wine from the list: Shiraz Merlot Chianti Other view script forms/doform6.php

7/9/ Sticky Checkboxes (1) Write a function:

7/9/ Sticky Checkboxes (2) Use the check function as follows Shiraz > Merlot > Chianti > view script forms/doform7.php

7/9/ Sticky Radio Buttons (1) Write a function:

7/9/ Sticky Radio Buttons (2) Use the check function as follows Shiraz > Merlot > Chianti > view script forms/doform8.php

7/9/ Sticky Drop Down List (1) Write a function:

7/9/ Sticky Drop Down List (2) Use the check function as follows >Shiraz >Merlot >Chianti >Other view script forms/doform9.php

7/9/ Generic form script (1) Form post and get parameters "; } ?> view script forms/general.php

7/9/ Generic form script (2) Try the script with the following query string ?name=fred&age=24&wines[]=shiraz&wines[]= merlot&wines[]=chianti e=fred&age=25&wines[]=shiraz&wines[]=m erlot&wines[]=chianti e=fred&age=25&wines[]=shiraz&wines[]=m erlot&wines[]=chianti

7/9/ Temp conversion input page output page

7/9/ Distinguishing GET and POST Distinguish between GET and POST methods to decide whether to display a form or use the results $request_method = $_SERVER['REQUEST_METHOD']; if ($request_method == 'GET') { // display form to get fahrenheit temperature } elseif ($request_method == 'POST') { // use submitted temperature to convert to // celsius and display the result }

7/9/ temp1.php (1) Fahrenheit to Celsius Conversion Fahrenheit to Celsius Conversion " method="POST"> Fahrenheit Temperature:

7/9/ temp1.php (2) Another conversion "; } ?> view script forms/temp1.php

7/9/ Another approach Another way to decide whether to display the form or do the calculations and display the results: use button name. It will be sent to the PHP script only if it is clicked. if (isset($_REQUEST['convert'])) // button clicked { // do the conversion and display results } else { // display the form }

7/9/ temp2.php (1) Fahrenheit to Celsius Conversion Fahrenheit to Celsius Conversion Another conversion " }

7/9/ temp2.php (2) else { ?> " method="POST"> Fahrenheit Temperature: view script forms/temp2.php

7/9/ Another approach Another way is to use an HTML page with some dynamic parts. The output is displayed on same page as the form.

7/9/ temp3.php (1) Fahrenheit to Celsius Conversion Fahrenheit to Celsius Conversion " method="POST"> Fahrenheit temperature: " />

7/9/ temp3.php (2)