Ch4 - PHP
Ch4 - PHP
Ch4 - PHP
Chapter 4
PHP (Hypertext Preprocessor)
2019
Tran Cong An
([email protected])
Content
1. Introduction
2. PHP language basics
3. PHP functions
4. OOP in PHP
5. PHP and forms
6. PHP and MySQL
7. Cookies and sessions
8. Advanced PHP techniques (file upload, pagination, AJAX, etc.)
9. Appendix
2
Introduction to PHP
3
Introduction to PHP
What is PHP?
- Basic characteristics:
• Widely used and open source scripting language
• Executed on the server
• Dynamically typed and purely interpreted
• Supported by most of popular web servers (IIS, Apache, etc.) and
OS (Windows, Linux, MacOS, etc.)
4
Introduction to PHP
What is PHP?
5
Introduction to PHP
6
Introduction to PHP
7
Introduction to PHP
8
Introduction to PHP
Why PHP?
9
Introduction to PHP
10
PHP Language Basics
11
PHP Language Basics
Basic Syntax
The PHP processor has two modes: copy (HTML) and interpret (PHP)
12
PHP Language Basics
Basic Syntax
Copy &
Interpret
copy (HTML)
interpret (PHP)
copy (HTML)
13
PHP Language Basics
Variables
14
PHP Language Basics
Variables
15
PHP Language Basics
Variables
Result: 15
16
PHP Language Basics
Variables
- Static variables:
• Created inside a function
• Not deallocated/deleted when the function completed (i.e. their
values are retained for further usage in the latter runs)
- Variable datatype:
• Automatically assigned depending on its value (loosely typed)
• To get the datatype od a variable: gettype(var_name)
• Functions to check variable type: is_bool(),
is_int(),is_float(), is_double(), is_string(),
is_object(),is_array(), is_numeric(), is_resource(),
is_null(),isset(), empty()
17
PHP Language Basics
Variables
18
PHP Language Basics
PHP Outputs
19
PHP Language Basics
Datatypes
20
PHP Language Basics
Operators
21
PHP Language Basics
Strings
22
PHP Language Basics
Constants
23
PHP Language Basics
Control Statements
- Condition statements:
• if ... else
• switch ... case
• ?
- Loops:
• while
• do … while
• for
• foreach
24
PHP Language Basics
25
PHP Language Basics
26
PHP Language Basics
27
PHP Language Basics
28
PHP Language Basics
29
Functions
30
Functions
Declarations
31
Functions
Example
32
Functions
Function Arguments
33
Functions
Function Arguments
34
Functions
Function Arguments
35
Functions
Function Arguments
36
Functions
37
Functions
Return Values
38
Functions
- Add a colon “:” and the type right before the “{” in the
function declaration to specify the function return type
- To enable strict requirement: declare(strict_types=1);
39
Arrays
40
Arrays
Arrays
41
Arrays
Indexed Arrays
- Declaration:
• Empty array: $arr_name = array()
• Array with initial values: $arr_name = array(val1, val2,…);
- Access (set/get) array elements: $arr_name[index]
- Get array length: count($arr_name)
- Loop through an array:
• for loop (usually combined with the count() function)
• foreach ($arr_name as $var) loop
Indexed Arrays
43
Arrays
Associative Arrays
44
Arrays
45
Arrays
Multi-dimensional Arrays
46
Arrays
Multi-dimensional Arrays
47
Arrays
Multi-dimensional Arrays
orange banana
apple
48
OOP in PHP
50
OOP in PHP
Class Definition
- Syntax:
51
OOP in PHP
Creating Objects
52
OOP in PHP
Object Assignment
53
OOP in PHP
Static Members
54
OOP in PHP
55
OOP in PHP
56
OOP in PHP
Inheritance
- Child class inherits all members of the parent class (but it can
access public and protected members)
- Child class can override methods of the parent class
- Access parent members from the child class:
• parent::property
• parent::method()
57
OOP in PHP
Inheritance
58
OOP in PHP
Further Reading
59
PHP and Forms
60
PHP and Forms
HTML Forms
• action: PHP page that will receive and process form data
• method: HTTP method that is used to send data to server
o GET: form data is sent via the URL parameters
o POST: form data is sent inside the request package body)
• Form data is sent to server in the form of an array (key => value)
where key is the name of the control and value is the input data
61
PHP and Forms
- The PHP global variables $_GET and $_POST are used to collect
form data (corresponding to the GET and POST method)
62
Checking Data Existence
63
PHP and MySQL
64
PHP and MySQL
65
PHP and MySQL
66
PHP and MySQL
67
PHP and MySQL
68
PHP and MySQL
Creating Connections
69
PHP and MySQL
Creating Connections
Selecting DB
71
PHP and MySQL
72
PHP and MySQL
- Prepared statements:
• mysqli::prepare($query): create prepared statement
o Returns a mysqli_stmt object or FALSE if an error occurred
• mysqli_stmt::bind_param($type, $var [, $var…]): binds
variables to a prepared statement as parameters
o Returns TRUE on success; FALSE on failure
o Parameter types: i (integer), d (double), s (string), b (blob)
• mysqli_stmt::execute(): execute a prepared query
o Returns TRUE on success; FALSE on failure
74
PHP and MySQL
75
PHP and MySQL
76
PHP and MySQL
77
PHP and MySQL
78
PHP and MySQL
79
PHP and MySQL
80
PHP and MySQL
81
PHP and MySQL
- Search function:
82
PHP and MySQL
- Application interface:
83
PHP and MySQL
- Main page:
84
PHP and MySQL
85
PHP and MySQL
86
PHP and MySQL
87
PHP and MySQL
88
PHP and MySQL
- Add the “Edit Book Detail” for the Book Manager Application
as follow:
• Add an Edit button next to the Delete button
• When the user click the Edit button, open a new page that
allows user to edit the details of the selected book (update page)
• In the update page, user can
o Edit and Save the change
o Go back to the home page
89
Cookies and Sessions
90
Cookies and Sessions
What is Cookie?
91
Cookies and Sessions
92
Cookies and Sessions
Cookie Example
93
Cookies and Sessions
Sessions
94
Cookies and Sessions
95
Cookies and Sessions
97
File Upload
98
File Upload
Setting Up
99
File Upload
- Form attributes:
• Method: POST
• Encryption method: enctype=multipart/form-data
• File select control: <input type=file ...>
100
File Upload
101
File Upload
Validation (Server)
102
File Upload
File Storage
- Related functions:
• move_uploaded_file(tmp_file, persistent_file): moves
an uploaded file to a new location (returns TRUE on success)
• file_exists(filename): check whether a file or directory
exists (returns TRUE on success)
103
File Upload
104
File Upload
upload-form.html
upload.php
105
File Upload
106
File Upload
- Upload form:
107
File Upload
108
File Upload
109
File Upload
110
Pagination
112
Pagination
113
File Upload
Case Study
114
File Upload
- Main page:
• Show the form to get search keywords + Search button
• Call search() function to search for books and get the
pagination information
• Call page_nav_link() function to create links to Prev/Next page
- PHP functions:
• compute_paging(search_kw): computer pagination parameters
• search(kw): search and return search result and pagination info
• page_nav_links(paging_info, search_kw): create links to
Prev/Next page
115
File Upload
Main page
116
File Upload
Function compute_paging()
117
File Upload
Function search()
118
File Upload
Function search()
119
AJAX
Asynchronous JavaScript And XML
120
AJAX
What is AJAX?
121
AJAX
Synchronous Communication
122
AJAX
Asynchronous Communication
- Users can interact with the web page while the browser is
communicating with web server
123
AJAX
Internet
Internet
HttpRequest returned data
XMLHttpRequest • Create a response using JS
object and send data back • Update page
• Sent HttpRequest to the browser content
124
AJAX
XMLHttpRequest Object
125
AJAX
XMLHttpRequest Object
- Request examples:
• GET request:
• POST request:
Content-type application/x-www-form-urlencoded
126
AJAX
XMLHttpRequest Object
- Request examples:
• POST with JSON data:
127
AJAX
XMLHttpRequest Object
- Server response:
• readyState: the status of the XMLHttpRequest object
(0: request not initialized, 1: server connection established, 2: request
received, 3: processing request, 4: response is ready)
• onreadystatechange: a function to be execute when the
readyState changes
128
AJAX
XMLHttpRequest Object
- Server response:
• status, statusText: status of the XMLHttpRequest object
(200: "OK", 403: "Forbidden", 404: "Page not found")
• responseText: contains the response data as a string
• responseXML: contains the response data as XML data
• getResponseHeader(header): returns specific header
information from the server
• getAllResponseHeaders(): Returns all the header information
from the server
129
AJAX
loadDoc() Function
130
AJAX
131
AJAX
132
AJAX
133
AJAX
̣ ̉
❶ ❷
̣ ệ
134
AJAX
135
AJAX
136
AJAX
137
AJAX
138
Question?
Chapter 3 – JavaScript
139
Appendix
140
Appendix – Simple CRUD Application
Application GUI
141
Appendix – Simple CRUD Application
Application GUI
142
Appendix – Simple CRUD Application
Application GUI
143
Appendix – Simple CRUD Application
Application GUI
144
Appendix – Simple CRUD Application
145
Appendix – Simple CRUD Application
Connect to MySQL
146
Appendix – Simple CRUD Application
147
Appendix – Simple CRUD Application
Index Page
148
Appendix – Simple CRUD Application
149
Update Form Generation (update.php)
150
Update Employee Information
151
Login, Logout and Permission Checking
152
The End!
153