CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

Web forms and CGI scripts Dr. Andrew C.R. Martin
Tutorial 6 Creating a Web Form
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
Faculty of Sciences and Social Sciences HOPE PHP – Working with Input Stewart Blakeway FML 213
JavaScript Forms Form Validation Cookies CGI Programs.
Python and Web Programming
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.
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 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.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
JavaScript Form Validation
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.
Week 4  Using PHP with HTML forms  Form Validation  Create your own Contact form Please Visit:
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.
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
INTERNET APPLICATION DEVELOPMENT For More visit:
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Homework for October 2011 Nikolay Kostov Telerik Corporation
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
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.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
PHP meets MySQL.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
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.
LOGO FORMs in HTML CHAPTER 5 Eastern Mediterranean University School of Computing and Technology Department of Information Technology ITEC229 Client-Side.
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:
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
HTML Form and PHP IST Review of Previous Class HTML table and PHP array Winner is chosen randomly using rand() function.
PHP Form Introduction Getting User Information Text Input.
Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
HTLM Forms CS3505. Form Handling in Browser html User Files out form WEbBROWSErWEbBROWSEr User read response submit Get URL?input html Get file html script.
Part 2 Lecture 9 PHP Superglobals and Form Handling.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your.
Higher Computing Science Coding the Web: HTML, JavaScript, PHP and MySQL.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
HTML FORM AND PHP IST 210: Organization of Data IST210 1.
INTERNET APPLICATIONS CPIT405 Forms, Internal links, meta tags, search engine friendly websites.
CGS 3066: Web Programming and Design Spring 2016 PHP.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
JavaScript, Sixth Edition
PHP (Session 1) INFO 257 Supplement.
Arrays: Checkboxes and Textareas
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 ©
Web Programming– UFCFB Lecture 17
Intro to PHP & Variables
Presentation transcript:

CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS

FORM VALIDATION Useful functions: NameDescriptionBest use empty($var)Returns TRUE if the variable has been set and is not NULL Text input isset($var)Returns TRUE if the variable hasn't been set, contains a NULL value, or contains an empty string. Non-text input: radio buttons, check boxes, submit. etc. is_numeric ($var)Returns TRUE if the variable is a number or a string that can be converted to a number

FORM HANDLING REVISITED Recall from Chapter 2 form handling with two pages: One page displays the form The second page processes it Instead, both parts can be written into one page with a conditional: if the form has been submitted process the data else display the form

BOOK EXAMPLE REVISED <?php // Check for form submission: if (isset($_POST['submit'])) { //calculate and print results } ?> Trip Cost Calculator Complete code:

REVISED BOOK EXAMPLE WITH FORM VALIDATION // Check for form submission: if (isset($_POST['submit'])) { // Minimal form validation: if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) { //calculate and print results } else { //invalid input echo ' Error! Please enter a valid distance, price per gallon, and fuel efficiency. ';} } Complete code:

STICKY FORMS The form remembers your previous entries. Text input: use the value attribute to print the variable if it exists. Radio buttons or check-boxes: add the code checked="checked" to the input tags if that value was selected. Textarea input: since there is no value attribute, just print the variable (if it is empty, nothing will show.) To preset a selection list, use the selected attribute. Complete code:

INCORPORATING EXTERNAL FILES include() and require() are equivalent when there are no problems – they differ in how they handle errors: include() – if include() fails, a warning will display, but the script will continue to run require() – if require() fails, and error is displayed, and the script is halted Relative or absolute referencing may be used – relative is preferable include_once() and require_once() are available for more complex code but cause extra work and thus potential slow- downs

ABSOLUTE VS RELATIVE PATHS An absolute path references a file starting from the root directory of the server: include('C:/php/includes/file.php'); include('/usr/home/public_html/php/includes/file.php'); A relative path uses the referencing (parent) file as the starting point. It will remain accurate even if the site is moved to another server. To move up one folder, use two periods To move into a folder, use its name followed by / include('../ex2/file.php');

SEPARATING PAGE PARTS

header.html

footer.html

index.php

A SIMPLE FILE STRUCTURE index.php includes (folder) header.htmlfooter.htmlstyle.css images (folder)

MORE COMPLEX FILE STRUCTURES Use MVC pattern: Model: contains files that manage the data or that interface with the database View: contains the files that represent the user interface Controller: contains files that receive the HTTP requests from browsers

USER-DEFINED FUNCTIONS Function names are case insensitive (unlike variable names) Syntax: function function_name() { // function code } Uses associate repeated code with one function call separate out sensitive or complicated processes from other code make common code bits easier to reuse

FUNCTIONS AND ARGUMENTS EXAMPLE Use a function to create the radio buttons in the car calculator program: // This function creates a radio button. // The function takes one argument: the value. // The function also makes the button "sticky". function create_gallon_radio($value) { // Start the element: echo '<input type="radio" name="gallon_price" value="'. $value. '"'; // Check for stickiness: if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == $value)) { echo ' checked="checked"'; } echo " /> $value "; // Complete the element } // End of create_gallon_radio() function.

CALLING THE FUNCTION Distance (in miles): " /> Ave. Price Per Gallon: <?php create_gallon_radio('3.00'); create_gallon_radio('3.50'); create_gallon_radio('4.00'); ?> …

RETURNING VALUES FROM FUNCTIONS PHP functions may or may not return values print will return a 1 (success) or a 0 (fail) echo does not return anything Use return() Example: function calculate_trip_cost($miles, $mpg, $ppg) { // Get the number of gallons: $gallons = $miles/$mpg; // Get the cost of those gallons: $dollars = $gallons/$ppg; // Return the formatted cost: return number_format($dollars, 2); } // End of calculate_trip_cost() function.