You are on page 1of 19

-1-

Chapter 5

Form Control for Passing


Values

-2-
ផលសេ%មចរ ំពឹងទុនៃនេមេរ1ន

Ø ករេ$បី Method នង
ិ Action របស់ Form
Ø ករទទួលតៃម2ពី Method GET នង
ិ POST
Ø Form Validation
Ø Form Require Fields
Ø កររក5តៃម2ទុកកនុង Form េពល Submit
Ø េចះេ$បី Session នង
ិ Cookie
Ø ករេ$ប$ី បស់ File

-3-
ករ%បមូលទិននន័យេចញពី Form

Ø កនុង PHP េដម


ី ប$ី បមូលទន
ិ នន័យពី Form េគេ$ប$ី បស់វធ
ិ C
ី DសEពរី គឺ
GET នង
ិ POST។
§ GET េបះទន
ិ នន័យHមរយៈ URL parameters
§ POST េបះទន
ិ នន័យHមរយៈ HTTP POST method។

-4-
ករេ%ប%បស់ GET

Ø ព័ត៌មនែដលបនបញជូ នពី Form Hមរយៈ GET Method គប


ឺ ងPញ
ឱយអនកេមល
ី េឃញ
ី ($គប់ Variable នង
ិ តៃម2គ$ឺ តVវបនបងPញេនកនុង
URL)។ GET Xចបញជូ នទន
ិ នន័យបន$បែហល 2000 Characters
ប៉ុេ[\ះ។
Ø GET េ$បស
ី $មប់ែតករបញជូ នទន
ិ នន័យែដលមន
ិ សមងត់បុេ៉ [\ះ។
Note: GET គម
ឺ ន
ិ គួរេ$បស
ី $មប់បញជូ ន Password ឬព័ត៌មនែដលមន

ចង់ឱយេគដង
ឹ េឡយ
ី ។

-5-
ករេ%ប%បស់ POST

Ø ព័ត៌មនែដលបនបញជូ នពី Form មួយHមរយៈ POST Method គឺ


មិន$តVវបនបងPញឱយេឃញ
ី េឡយ
ី (aល់ names/values ទំងអស់គឺ
$តVវបន Embed េនកនុង Body ៃន HTTP Request) េហយ
ី dមិន
កំណត់ចំនួនទន
ិ នន័យែដល$តVវបញជូ នេទ។

-6-
Form Required Fields

Ø េនេពលេយង
ី បេងកីត Form មួយេដីមប$ី បមូលព័ត៌មន េហយ
ី Field
ខ2ះទមទរ$តVវែតបញចូ លiច់ខតមន
ិ Xចខkះបន ដូេចនះេយង
ី Xច
កំណត់ Validation ឱយ Field ទំងេនះេនេពលែដលមន
ិ បញចូ លdនង

បងPញ Message $បប់ឱយអនកបញចូ លបនដឹង។
Ø empty(variable) $តmតពន
ិ ត
ិ យេមល
ី Variable ទេទរឬអត់។ តៃម2
ែដលចត់ទុកថ empty មនដូចជ 0, 0.0, "0", "", NULL, FALSE,
array()

-7-
Form Required Fields

-8-
Forms Validate Name E-mail and URL

ី Xចកំណត់ Field េទHមទ$មង់ Format របស់dមួយៗដូចជ


Ø េយង
Name, Email នង
ិ URL ជេដម
ី ។
Ø preg_match("/^[a-zA-Z ]*$/",variable)
Ø filter_var(variable, FILTER_VALIDATE_EMAIL)
Ø preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-
9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",variable)

-9-
កររក?ទិននន័យទុកកនង
ុ Form េពល Submit

Ø េនេពលែដលេយង
ី បនបញចូ ល$គប់ Field ទំងអស់បន$តម
ឹ $តVវHម
ករកំណត់ Validation េហយ
ី េនះ េដីមបប
ី ងPញ តៃម2េនកនុង Field
នម
ី ួយៗបនទប់ពី User បនចុច Submit Form េយង
ី $តVវសរេសរ
PHP Script បែនថមេនកនុង Value Attribute របស់ Field នីមួយៗ
េដម
ី បច
ី ប់យកតៃម2បញចូ លកនុង Field ទំងេនះ។

-10-
កររក?ទិននន័យទុកកនង
ុ Form េពល Submit

-11-
ករសិក?ពី Cookies

Ø Cookie គជ
ឺ File តូចមួយែដល Server បេងកីតdេនេលី Browser
េហយ
ី រក5dទុកកនុងកុំពយូទ័ររបស់អនកេ$ប$ី បស់។ ជទូេទេគេ$បd

ស$មប់សមគល់ User។ ឧទហរណ៍ដូចជករចងចំ Username
នង
ិ Password ជេដម
ី ។

-12-
ករសិក?ពី Cookies

Ø ករបេងកីត Cookie
setcookie(name, value, expire)
Ø ករែកតៃម2 Cookie
setcookie(name, value, expire)
Ø ករលុប Cookie
setcookie(“name", "", time() - 3600) // expire 1 hour ago
Øេដីមបa
ី ប់ចំនួន Cookie បនបេងកីត
count ($_COOKIE)

-13-
ករសិក?ពី Sessions

Ø Session គជ
ឺ កររក5ទុកព័ត៌មនកនុង Variable េដីមបX
ី េ$ប$ី បស់
ឆ2ងេទ Page េផzងេទ{តបន។
Ø Session Xចរក5ទុកតៃម2បនែតេពលែដល Browser កំពុងែតេបក

ប៉ុេ[\ះ េនេពលបិទ Browser dនឹងលុបេចលេiយសk័យ$បវតEិ។

-14-
ករសិក?ពី Sessions

Ø ករបេងកីត Session
session_start() // Start Session
$_SESSION[‘var-name’] // Set Variable to Session
Øករែកតៃម2 Session
េដីមបែី កតៃម2របស់ Session $គន់ែតសរេសរតៃម2ថមីជន់េលីតៃម2ចស់
Ø ករលុប Session
session_unset() // Remove all session variable
session_destroy() // Destroy the session

-15-
ដំេណរករ PHP ជមួយ File

Ø Open File
fopen(“file”, “modes”)
Ø Close File
fclose(filename)
ØRead File
fread(filename, filesize(“file”))
Ø Create File
fopen(“filename”, “w”)
ØWrite File
fwrite(“filename”, “text”)

-16-
File Modes

Modes Description

r Open a file for read only. File pointer starts at the beginning of the file

w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist.
File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of
the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists

r+ Open a file for read/write. File pointer starts at the beginning of the file

w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't
exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end
of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists

-17-
THANK YOU J

-18-
-19-

You might also like