Chapter 6 Getting Data from the Client (PHP Part)
Chapter 6 Getting Data from the Client (PHP Part)
2
Introduction (cont…)
• Form controls include text fields, password fields, hidden
field, radio buttons, checkboxes, textarea fields, drop-down
menus, listbox, file uploads, submit button, image button,
reset button, etc.
• To build a form, you must have at least the following
elements:
An opening <form> and closing </form> tag
A submission type specifying either a Get or Post method
One or more input fields
The destination URL to which the form data is to be
submitted. 3
Form tags
• <FORM>
• <INPUT>
• <SELECT>
<OPTION> (it is a sub tag for <SELECT> tag)
• <TEXTAREA>
Jarane 4
Retrieving Submitted Data
• To collect form data we use PHP superglobals with control
name as index:
• $_GET: if form method is set to get.
• $_POST: if form method is set to post.
• $_REQUEST: if form method is set either get or post.
• $_FILES: is used with upload file to collect file info.
5
PHP Code
<?php
echo ("User name: ") . $_POST ['name'];
echo "<br>Password: " . $_POST ["pword"];
?>
6
Important notes
• Superglobals are in uppercase letters.
• You can use single or double quotes with superglobals
indexes.
• Form field names are case sensitive.
7
Textbox, password, and
textarea fields
<form method="post" enctype="multipart/form-data">
<input type="text" name="fullName"><br>
<input type="password" name="pinCode"><br>
<textarea name="comment" cols="30" rows="6"></
textarea><br>
<input type="reset" value="Reset form">
<input type="submit" name="submit" value="Register">
</form>
8
PHP Code
<?php
echo ("User name: ") . $_POST ['fullName'];
echo "<br>Password: " . $_POST ['pinCode'];
echo ("<br>Comment: ") . $_POST ["comment"];
?>
9
Textbox, password, and Radio
button fields
<form method="post" enctype="multipart/form-data">
<input type="text" name="fullName"><br>
<input type="password" name="pinCode"><br>
<input type="radio" name="gender" value="male">Male
11
checkbox fields (html code)
<form method="post" enctype="multipart/form-data">
<input type="checkbox" name="faculties[]" value="engineering
">Engineering
<input type="checkbox" name="faculties[]" value="medi
cine">Medicine
<input type="checkbox" name="faculties[]" value="com
puter sciences">Computer Sciences
<input type="submit" name="submit" value="Register">
</form>
12
checkbox fields (php code)
<?php
foreach ($_POST['faculties'] as $f)
echo ("$f, ");
?>
13
Drop-down (single choice)
<form method="post" enctype="multipart/form-data">
<select name="pob">
<option value="Muqdisho">Muqdisho</option>
<option value="Hargeysa">Hargeysa</option>
<option value="Kismayo">Kismayo</option>
<option value="Marko">Marko</option>
</select> <br>
<input type="submit" name="submit" value="Register">
</form>
14
Example
<?php
if (!empty($_POST['pob']))
echo ("<br>Place of birth: ". $_POST['pob']);
?>
15
Listbox (multiple choice)
<form method="post" enctype="multipart/form-data">
<select name="residence[]" multiple>
<option value="Muqdisho">Muqdisho</option>
<option value="Hargeysa">Hargeysa</option>
<option value="Kismayo">Kismayo</option>
<option value="Marko">Marko</option>
</select> <br>
<input type="submit" name="submit" value="Register">
</form>
16
Example
<?php
if (!empty($_POST['residence'])) {
foreach ($_POST['residence'] as $r)
echo ("$r, ");
}
?>
17
Upload Files in PHP
Configuring The "php.ini" File
• There are several settings in PHP’s configuration file (php.ini)
that dictate how PHP handles uploads, specifically stating
how large of a file can be uploaded and where the upload
should temporarily be stored (you can browse php info file).
19
Php.ini file Configuration (1 – 3)
First, ensure that PHP is configured to allow file uploads and
check the following in your "php.ini":
• Whether to allow HTTP file uploads (On is default).
file_uploads = On
• Temporary directory for HTTP uploaded files ("C:\xampp\
tmp" is default).
upload_tmp_dir = "C:\xampp\tmp"
• Maximum allowed size for uploaded files (2MB is default).
upload_max_filesize = 2M
20
Php.ini file Configuration (2 – 3)
• Maximum number of files that can be uploaded via a single
request (20 files is default).
max_file_uploads = 20
• Maximum size of POST data that PHP will accept (8MB is
default).
post_max_size = 8M
• Maximum amount of time each script may spend parsing
request data (60 seconds is default).
max_input_time = 60
21
Php.ini file Configuration (3 – 3)
• Maximum amount of memory a script may consume (128MB
is default).
memory_limit = 128M
• Maximum execution time of each script, in seconds (30
seconds is default).
max_execution_time = 30
22
Temporary Directory
• Also final storage directory must exist with the correct
permissions. Initially files are uploaded into a temporary
directory ("C:\xampp\tmp") and then relocated to a target
destination by a PHP script.
• Information in the phpinfo.php page describes the default
configurations of xampp server.
23
Create The HTML Form
Create an HTML form that allow users to choose the image file
they want to upload.
Some rules to follow for the HTML form above:
• Make sure that the form uses method="post"
• The form also needs the following attribute:
enctype = "multipart/form-data"
It specifies which content-type to use when submitting the
form.
Without the requirements above, the file upload will not
work. 24
Example
<form method="post" enctype="multipart/form-data">
<label>Secondary certificate: </label>
<input type="file" name="scertificate">
<label>passport size photo: </label>
<input type="file" name="photo">
<input type="submit" name="submit" value="Upload">
</form>
25
process of uploading files (1 of
2)
• The user opens the page containing a HTML form featuring a
text files, a browse button and a submit button.
• The user clicks the browse button and selects a file to upload
from the local PC.
• The full path to the selected file appears in the text filed then
the user clicks the submit button.
• The selected file is sent to the temporary directory on the
server.
26
process of uploading files (2 of
2)
• The PHP script that was specified as the form handler in the
form's action attribute checks that the file has arrived and
then copies the file into an intended directory.
• The PHP script confirms the success to the user.
27
File Type & Permissions
• As usual when writing files it is necessary for both temporary
and final locations to have permissions set that enable file
writing. If either is set to be read-only then process will fail.
• An uploaded file could be a text file or image file or any
document.
28
Create the Upload File PHP Script
• Use !empty with control name, ($_FILES['']), to make sure
some file is selected.
• $_FILES[' '] is an array with the file information.
• $_FILE[] can have second argument such as name, size,
tmp_name, and type where:
[name] is the name of the file.
[size] is the size of the file.
[tmp_name] is the temporary location where the file is
uploaded before its final destination.
[type] is the type of the file.
29
Example
$_FILES['myFile']['name'];
$_FILES['myFile']['size'];
$_FILES['myFile']['tmp_name'];
$_FILES['myFile']['type'];
30
Move file
• To move the file from the temporary folder (tmp_name) to
another location in the server we will be using
move_uploaded_file ( ) function.
• It takes two arguments, temporary file name, destination
folder name and the name of the file after the upload
process (in the destination folder).
31
Example
<?php
if (move_uploaded_file($_FILES['scertificate']
['tmp_name'], "Docs/". $_FILES['scertificate']['name']))
echo ("<br>Certificate has been uploaded successfully.");
if (move_uploaded_file($_FILES['photo']
['tmp_name'], "Docs/". $_FILES['photo']['name']))
echo ("<br>Passport has been uploaded successfully.");
?>
32
file_exists ( ) function
• You can use file_exists ( ) function to check whether or not a
file exists in the directory.
• Syntax: file_exists (path and file name)
• Example:
elseif (file_exists ("Docs/". $_FILES['scertificate']['name']))
echo ("<br>Sorry, file: ". $_FILES['scertificate']
['name']. " already exists.");
elseif (file_exists ("Docs/". $_FILES['photo']['name']))
echo ("<br>Sorry, file: ". $_FILES['photo']
['name']. " already exists.");
33
Check file size
• To check for size we can use size property of control.
• For example:
elseif (!$_FILES['scertificate']['size'])
echo ("File: ". $_FILES['scertificate']['name']. " is too big.");
elseif (!$_FILES['photo']['size'])
echo ("File: ". $_FILES['photo']['name']. " is too big.");
34
Limit file extensions
• To limit file extensions allowed in the upload process to
predefined list of extensions use explode, strtolower, end
and in_array functions as follows:
$extensions = array ("docx", "pdf");
$name_ext = explode (".", $_FILES['scertificate']['name']);
$ext = strtolower(end ($name_ext));
if (!in_array ($ext, $extensions))
echo ("<br>File extenssion: $ext is not allowed");
35
Uploading multiple files
• It is also possible to upload multiple files simultaneously and
have the information organized automatically in arrays for
you.
• To do so, you need to use the same array submission syntax
in the HTML form as you do with multiple selects of
dropdown menu and checkboxes.
36
Example
HTML code:
<form method="post" enctype="multipart/form-data">
<input type="file" name="myFiles[]" multiple>
<input type="submit" name="submit" value="Upload">
</form>
37
PHP code
<?php
for ($i = 0; $i < count($_FILES['myFiles']['name']); $i++)
if (move_uploaded_file($_FILES['myFiles']['tmp_name']
[$i], "Docs/". $_FILES['myFiles']['name'][$i]))
echo ("<br>Files uploaded successfully");
?>
38
Form Validation Checklist (1-3)
• Controls validation
Isset (radio button & checkbox)
Empty (textbox, password, dropdown menu, listbox, and
textarea, file upload)
And with full stop (checkbox and listbox)
Sort checkbox & listbox.
Uppercase first latter (radio button, checkbox, dropdown
menu, listbox)
39
Form Validation Checklist (2-3)
• File upload validation
File extension
Size
Already exists
Uploaded file name as the user name
Display image on the screen after upload
Upload multiple files
40
Form Validation Checklist (3-3)
• Keep form data after submit
• And many more
41
practice makes perfect
42