Input Types:
The "Name" attribute must be defined for every <input> element.
Text Field
HTML Code:
<form action = "testing.php" method="POST">
Textbox description:
<input type = "text" name = "nickname">
</form>
Textbox description:
Password Field
HTML Code:
<form action = "testing.php" method="POST">
Password:
<input type = "password" name = "pwd">
</form>
Password:
Date Field
HTML Code:
<form action = "testing.php" method="POST">
Date of Birth:
<input type = "date" name = "bday">
</form>
Date of Birth:
Dropdown Selection Field
HTML Code:
<form action = "testing.php" method="POST">
Select your Region:
<select name = "region">
<option value = "NT">New Territories</option>
<option value = "KLN">Kowloon</option>
<option value = "HK">Hong Kong Island</option>
</select>
</form>
Select your Region:
Checkbox Field
HTML Code:
<form action = "testing.php" method="POST">
Hobbies:<br>
<input type = "checkbox" name = "hobbies[]" value = "coding">Coding<br>
<input type = "checkbox" name = "hobbies[]" value = "music">Music<br>
<input type = "checkbox" name = "hobbies[]" value = "reading">Reading<br>
</form>
Hobbies:
Coding
Music
Reading
Radio Button
HTML code:
<form action = "testing.php" method="POST">
Select your gender:
<input type = "radio" id = "male" name = "gender" value = "male">
<label for = "male">Male</label><br>
<input type = "radio" id = "female" name = "gender" value = "female">
<label for = "female">Female</label><br>
</form>
Select your gender:
Submit
HTML code:
<form action = "testing.php" method="POST">
<input type="submit" value="Submit">
</form>
Reset
HTML code:
<form action = "testing.php" method="POST">
<input type="reset" value="Reset">
</form>
Submitting an HTML Form
Sending Form Data from Client to Server
Step 1: Organising the Form's Data into Index-Value Pairs
Step 2: Client Browser prepares an HTTP Request
Step 3: Add Form Data to HTTP Request
Comparing GET and POST
| GET | POST | |
|---|---|---|
| Desired Action | To fetch a specific resource from the server | To perfrom a specific action which effects a change on the server's state |
| How Form Data are contained in HTTP Request | Encoded as Query String which forms part of the URL Visible to user on address bar |
Contained as Request Body Not visible to the user |
| Bookmark/Browsing History | Can be bookmarked Saved in Browsing History in some browsers |
Not saved or bookmarked |
| Length Limit | Usually 2KB due to length limit of URL Only text data allowed |
|
| Able to be Cached? | Responses to some GET method requests can be cached | Responses to POST method requests are not cached in general |
PHP Superglobals
$_POST
When server receives a POST request, all data contained in the request body are gathered into $_POST, and the PHP file can access these values in its PHP code.
For example:
Code for file where data is inputted:
<form action = "height.php" method="POST">
Height:
<input type="text" value="">
</form>
<?php
$height = $_POST["height"];
if ($_POST["height"] < 160) {
echo "You're short";
}
?>
$_GET
When server receives a GET Method request, all data contained in the query string are gathered into $_GET, and the PHP file can access these values in its PHP code.
For example:
pastpaper.php:
<?php
$subject = $_GET["subject"];
if ($_GET["year"] < 2012) {
echo "No past papers before 2012";
}
?>
Data Validation
Client-Side Validation
The client's browser validates the user's input before the data are sent to the server
Server-Side Validation
The server validates user-inputted data after it is sent to the server
Common Functions used for Server-side validation:
| Validation Type | PHP function | Description |
| Presence Check | isset() | Returns true if the variable exists in runtime, regardless of its value |
| empty() | Returns true if the given variable either, (1) does not exist in runtime, or (2) is empty |
|
| Fixed Value Check | in_array() | Returns true if the value of the given variable exists in the array |
| Type Check | is_numeric() | Returns true if the given variable is a number or a number string |
| is_string() | Returns true if the given variable is a string |
|
| Length Check | strlen() | Returns the number of characters in the given string |
| Format Check | strpos() | Returns the position of the first occurence of a substring in a string. If the substring is not found, it returns false |
Cookies
How are cookies created?
Benefits of using Cookies in web application
Cookies can be used to provide a smoother user experience and store analytics data used for market research.
Risks involved with using Cookies