III. Làm việc với Form
III.1. Các biến SuperGlobal
III.2. Biến $_SERVER
III.3. Lấy thông tin từ form
III.4. Kết hợp mã HTML và PHP trong 1 trang
III.5. Sử dụng Hidden field để lưu trạng thái
III.6. Redirect
III.7. Form Upload file
III.1. Các biến SuperGlobal
Các biến global là các biến được khai báo ở mức ngoài cùng của PHP được
sử dụng chung cho mọi module
Các biến SuperGlobal là các biến (mảng) được PHP tạo ra để lưu các thông
tin cần thiết
Array Description
$_COOKIE Contains keys and values set as browser cookies
$_ENV Contains keys and values set by the script's shell context
$_FILES Contains information about uploaded files
$_GET Contains keys and values submitted to the script using the HTTP get
method
$_POST Contains keys and values submitted to the script using the HTTP post
method
$_REQUEST A combined array containing values from the $_GET, $_POST, and
$_COOKIES superglobal arrays
$_SERVER Variables made available by the server
$GLOBALS Contains all global variables associated with the current script
III.2. Biến $_SERVER
Variable Contains Example
$_SERVER['PHP_SELF'] The current script.
Suitable for use in
links and form
element action
arguments.
/phpbook/source/listing10.1.ph
p
$_SERVER['HTTP_USER_AGEN
T'] The name and
version of the client. Mozilla/4.6 (X11; I;Linux2.2. 6-
15apmac ppc)
$_SERVER['REMOTE_ADDR'] The IP address of
the client. 158.152.55.35
$_SERVER['REQUEST_METHO
D'] Whether the request
was GET or POST. POST
$_SERVER['QUERY_STRING'] For GET requests,
the encoded data
sent appended to the
URL.
name=matt&address=unknown
$_SERVER['REQUEST_URL'] The full address of
the request,
including query
string.
/phpbook/source/listing10.1.ph
p? name=matt
$_SERVER['HTTP_REFERER'] The address of the
page from which the
request was made.
http://p24.corrosive
III.2. Biến $_SERVER (2)
Đoạn lệnh sau liệt kê tất cả các thông tin của biến
$_SERVER
<?php
foreach ($_SERVER as $key=>$value) {
echo "[$key]=$value\n";
}
?>
III.3. Lấy thông tin từ form
III.3.1. Lấy thông tin từ form bằng các mảng
superglobal
III.3.2. Import thông tin của form vào các biến
global
III.3.3. Lấy thông tin của form bằng mảng do
người dùng định nghĩa