CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
Tutorial 6 Creating a Web Form
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
JavaScript Forms Form Validation Cookies CGI Programs.
CS 185C/286: The History of Computing October 31 Class Meeting Department of Computer Science San Jose State University Fall 2011 Instructor: Ron Mak
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
Figure 1. Hit analysis in 2002 of database-driven web applications Hits by Category in 2002 N = 73,873 Results Reporting 27% GME 26% Research 20% Bed Availability.
CS 174: Web Programming February 26 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
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.
Unit 7 – Working with Forms 1. Creating a form 2. Accessing the submitted data 3. Common operations on forms.
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
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.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
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.
Telerik Software Academy ASP.NET Web Forms Data Validation, Data Validators, Validation Groups Telerik Software Academy
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
CS 160: Software Engineering October 8 Class Meeting
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
PHP meets MySQL.
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Website Development with PHP and MySQL Saving Data.
CS 160: Software Engineering October 1 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 15 Class Meeting
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CS 174: Web Programming August 31 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Creating PHPs to Insert, Update, and Delete Data CS 320.
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
CS 174: Web Programming September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Form Handling IDIA 618 Fall 2014 Bridget M. Blodgett.
©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.
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.
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
Higher Computing Science Coding the Web: HTML, JavaScript, PHP and MySQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 160 and CMPE/SE 131 Software Engineering March 15 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
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,
Radoslav Georgiev Telerik Corporation
CS 157B: Database Management Systems II January 23 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Web Database Programming Using PHP
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Database Programming Using PHP
CS 153: Concepts of Compiler Design October 17 Class Meeting
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 ©
CMPE/SE 131 Software Engineering March 9 Class Meeting
A Very Brief Introduction to Relational Databases
Presentation transcript:

CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Hidden HTML Form Input Fields  So far, a data submitted by an HTML form have been explicitly entered by the user. text fields checkboxes radio button select menus  A “hidden” input field: Send data from an HTML form to the server in a way that is not displayed by the web page. 2

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Receiving Client Data by the Server  Send data to the server using get or post. Corresponding ways for PHP code on the server to receive the data:  Older ways to receive data is via the PHP superglobals: $_REQUEST combines $_GET and $_POST. 3 $first = $_GET ( "firstName"); $language = $_POST("language"); $direction = $_REQUEST("direction"); $first = filter_input(INPUT_GET, "firstName"); $language = filter_input(INPUT_POST, "language");

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Input Filtering  An optional third parameter specifies either a sanitizing filter or a validation filter. Example:  A sanitizing filter strips off certain characters.  A validating filter checks the input for validity. 4 if (!filter_input(INPUT_GET, " ", FILTER_VALIDATE_ )) { echo(" is not valid"); } else { echo(" is valid"); }

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Input Filtering, cont’d 5 Filter constantIDDescription FILTER_VALIDATE_BOOLEAN258Validates a boolean FILTER_VALIDATE_ 274Validates an address FILTER_VALIDATE_FLOAT259Validates a float FILTER_VALIDATE_INT257Validates an integer FILTER_VALIDATE_IP275Validates an IP address FILTER_VALIDATE_REGEXP272Validates a regular expression FILTER_VALIDATE_URL273Validates a URL FILTER_SANITIZE_ 517Removes all illegal characters from an address FILTER_SANITIZE_ENCODED514Removes/Encodes special characters FILTER_SANITIZE_MAGIC_QUOTES521Apply addslashes() FILTER_SANITIZE_NUMBER_FLOAT520Remove all characters, except digits, +- and optionally.,eE FILTER_SANITIZE_NUMBER_INT519Removes all characters except digits and + - FILTER_SANITIZE_SPECIAL_CHARS515Removes special characters FILTER_SANITIZE_STRING513Removes tags/special characters from a string FILTER_SANITIZE_STRIPPED513Alias of FILTER_SANITIZE_STRING FILTER_SANITIZE_URL518Removes all illegal character from s URL FILTER_UNSAFE_RAW516Do nothing, optionally strip/encode special characters FILTER_CALLBACK1024Call a user-defined function to filter data Default

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP is Object-Oriented  The object-oriented features and syntax of PHP resemble those of Java: classes and objects abstract classes inheritance interfaces  PHP also has traits. Add functionality to a class without inheritance. 6

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Classes 7 class Pet { public $name; function __construct($pet_name) { $this->name = $pet_name; } function eat() { /*... */ } function sleep() { /*... */ } function play() { /*... */ } } The constructor is always named __construct (two underscores). oo/Pet.php

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Inheritance 8 class Cat extends Pet { function play() { parent::play(); } Scope resolution operator ::  As with Java, a PHP class can inherit from at most one superclass. oo/Pet.php

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Objects 9 $cat = new Cat('Eliza'); $pet = new Pet('Norska'); $cat->eat(); $pet->sleep(); // Delete the objects unset($cat, $pet); oo/Pet.php

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Abstract Classes 10 abstract class Shape { abstract public function getArea(); abstract public function getPerimeter(); } require('Shape.php'); class Triangle extends Shape { private $_sides = array(); private $_perimeter = NULL; function __construct($s0 = 0, $s1 = 0, $s2 = 0) { /*... */ } public function getArea() { /*... */ } public function getPerimeter() { /*... */ } } oo/Shape.php oo/Triangle.php

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Interfaces 11 interface Crud { public function create($data); public function read(); public function update($data); public function delete(); } require('Crud.php'); class User implements Crud { /*... */ function create($data) { /*... */ } function read() { /*... */ } function update($data) { /*... */ } public function delete() { /*... */ } } oo/Crud.php oo/User.php

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Traits  Traits add functionality to a class without class inheritance. They help overcome some of the restrictions of single inheritance.  A class can use several traits.  Several classes can share traits. 12

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Traits, cont’d 13 trait Debug { public function dumpObject() { $class = get_class($this); $attributes = get_object_vars($this); $methods = get_class_methods($this); echo " Information about the $class object "; echo ' Attributes '; foreach ($attributes as $k => $v) { echo " $k: $v "; } echo ' '; echo ' Methods '; foreach ($methods as $v) { echo " $v "; } echo ' '; } oo/Debug.php

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Traits, cont’d require('Shape.php'); require('Debug.php'); class Rectangle extends Shape { use Debug; public $width; public $height; function __construct($w, $h) { $width = $w; $height = $h; } function getArea() { return $width * $height; } function getPerimeter() { return 2*($width + $height); } } oo/Rectangle.php

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak PHP Traits, cont’d 15 require('Rectangle.php'); $r = new Rectangle(42, 37); $r->dumpObject(); oo/traittest.php Demo

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Sample Midterm Question #1  Write the HTML and JavaScript code to allow a user to input a credit card number and then validate and reformat the number. The user must enter the card number with spaces such as and the web page should submit the number without spaces. Pop up an alert that displays the reformatted number that is being submitted, or the alert should display an error message if the user entered the number in the wrong format. 16

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Sample Midterm Question #1, cont’d 17

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Solution to Question #1 18 Enter your credit card number <input type = "text" value = "" id = "ccNumber" />

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Solution to Question #1, cont’d 19 function validate() { ccNumber = document.getElementById("ccNumber"); ccn = ccNumber.value; ccnRE = /(\d{4}) (\d{4}) (\d{4}) (\d{4})/; if (!ccn.match(ccnRE)){ alert("Invalid credit card number format."); return false; } else { ccn = ccn.replace(ccnRE, "$1$2$3$4"); ccNumber.value = ccn; alert("Submitted: " + ccNumber.value); return true; } }

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Sample Midterm Question #2 20  Normalize this table to 2NF.  Represent your normalized tables with a simple generic diagram or with a crow’s feet ER diagram. Show the field names, but you can leave off the data types. Identify the primary and foreign keys with (PK) and (FK), respectively, next to the field names. Indicate which table each foreign key refers to.

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Solution to Question #2 21 (FK)

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Sample Midterm Question #3  Write an SQL statement that makes this query: Who are all the teachers of student John Doe and what are the corresponding subjects? 22

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Solution to Question #3 23 mysql> SELECT teacher.first, teacher.last, subject -> FROM student, teacher, class, student_class -> WHERE student.first = 'John' AND student.last = 'Doe' -> AND student.id = student_id AND class.code = class_code -> AND teacher.id = class.teacher_id -> ; | first | last | subject | | Tom | Rogers | Java programming | | Art | Thompson | Data structures | | John | Lane | Software engineering | rows in set (0.02 sec)

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Sample Midterm Question #4  Write the PHP statements that include a prepared statement to make the same query for any student when given the student’s first and last name, and then return the result set as an associative array. You may assume that variable $con already contains the connection to the database, and that variables $first and $last contain the name of a student. 24

Computer Science Dept. Fall 2015: October 14 CS 174: Web Programming © R. Mak Solution to Question #4 25 $query = "SELECT teacher.first, teacher.last, subject ". "FROM student, teacher, class, student_class ". "WHERE student.first = :first ". "AND student.last = :last ". "AND student.id = student_id ". "AND class.code = class_code ". "AND teacher.id = class.teacher_id"; $ps = $con->prepare($query); $ps->bindParam(':first', $first); $ps->bindParam(':last', $last); $ps->execute(); $ps->setFetchMode(PDO::FETCH_ASSOC);