Professional Documents
Culture Documents
Basics of PHP - 2
Basics of PHP - 2
1. HTML forms
The user can submit information using a standard HTML form. It is just a standard HTML
feature, neither PHP nor JavaScript.
1.1 <FORM> tag
The HTML form must begin with <FORM> and must end with </FORM>
<FORM>
...
</FORM>
You can specify the following attributes to <form> tag
Optional attributes
method="POST" / method="GET"
This determines the method of transmission of form data to the Web server.
The default method is GET, and all the form values will be sent as the
parameter of URL.
action="destination_URL"
This determines the destination URL that the form will be submitted. This URL
must be a page that uses server-side technology such as PHP, ASP or CGI.
If omitted, the same page as form page (itself) will be the destination.
1.2 HTML form items
Almost all standard user interface elements can be creates by <INPUT> tag.
The value of any kind of element will be always sent to Web server in the same key=value
format.
You can get each value of these elements by using $_GET["key"] or $_POST["key"]
array depending on the method specified in <FORM> tag as follows
(1) Single-line text box
<input type="text" name="key">
Optional attributes
value="value" If you specify value, it will be the initial text displayed.
type="password"
type="hidden" If you specify hidden for type, then the text box will not be
displayed on screen. You can use this type of text box to store
any value temporarily in HTML page, then send back again to
the Web server. This is commonly used for PHP page that
1
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Specify width of text box in number of characters. Note that this does
not limit the number of characters user will enter. For that
purpose, use maxlength=n instead.
type="button" will create normal button with NO function assigned. You must write
JavaScript or other script to assign the behavior manually.
type="submit" will create submit button for the form. Clicking on this button will
send all the information entered in the form
type="reset" will create reset button for the form. Clicking on this button will clear all
the fields in the form
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Optional attributes
value="value" If you specify value, it will be the value to be sent instead of item.
selected
If you specify selected for one of <option> tags, then that item will be
initially selected. Only 1 <option> tag is allowed to be selected.
If you specify checked for one of option buttons, then that option
button will be initially selected. Only 1 option button is allowed to be
selected within a set of option buttons.
If you specify checked, then the check box will be initially checked.
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
// Free result
mysql_close($link);
// Closing connection
?>
</BODY></HTML>
(Required modification for real use)
ADMTC-UCSC-University of Colombo
No
.
Basics of PHP - 2
Date
Branch
Item
Category
Unit
Price
Unit
Profit
2002-07-01
2002-07-01
2002-07-02
2002-07-03
2002-07-04
Jakarta
Medan
Surabaya
Jakarta
Makassar
Trousers
Suites
Mesh Bag
Suites
Trousers
Clothes
Clothes
Accessory
Clothes
Clothes
78000
345000
29800
345000
78000
22000
147000
16800
147000
22000
148
2002-09-30
1
2
3
4
5
Jakarta
T-Shirt
Clothes
39800
Qty
12
4
10
8
34
10800
45
Sales
Profit
936000
1380000
298000
2760000
2652000
264000
588000
168000
1176000
748000
1791000
486000
echo "</td>n";
} else {
echo "<td>$cell</td>n";
}
}
echo "</tr>n";
}
echo "</table>n";
...
Now the output will be like this.
No
.
Date
Branch
Item
Category
Unit
Price
Unit
Profit
1
2
3
4
5
2002-07-01
2002-07-01
2002-07-02
2002-07-03
2002-07-04
Jakarta
Medan
Surabaya
Jakarta
Makassar
Trousers
Suites
Mesh Bag
Suites
Trousers
Clothes
Clothes
Accessory
Clothes
Clothes
78,000
345,000
29,800
345,000
78,000
22,000
147,000
16,800
147,000
22,000
148
2002-09-30
39,800
Jakarta
T-Shirt
Clothes
Qty
Sales
Profit
12
4
10
8
34
936,000
1,380,000
298,000
2,760,000
2,652,000
264,000
588,000
168,000
1,176,000
748,000
10,800
45
1,791,000
486,000
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Here is the way to create user-defined function to output table records as HTML table.
<HTML><BODY>
<?php
function show_MySQL_table
($host_name,
$user_name,
$password,
$database_name,
$sql)
{
/* Connect to MySQL database */
if (!$result) {
echo "<B>(Error in SQL)</B> " . mysql_error();
die;
}
...
mysql_close($link);
}
// Closing connection
?>
</BODY></HTML>
When
you
finished
modification,
show_MySQL_table.php.
then
you
should
save
the
file
as
the
new
file
as
new
name
6
with
.php
extension.
Upload
both
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
show_MySQL_table.php and this new file to Web server, then try browsing this new file.
2.4 Display of table that has relation to other tables
Suppose we normalized (optimized) structure of table for previous practice as follows.
Item, Category, Unit Price and Unit Profit fields can be separated as Item
table, then just use Item No. in main table as a pointer to Item table
Remove Sales and Profit field because they can be calculated from other
values
sales2 table
No
.
Date
Branch
No.
Item
No.
Qty
Branch
No.
Branch
2002-07-01
2002-07-01
2002-07-02
2002-07-03
2002-07-04
1
3
2
1
4
2
3
6
3
2
12
4
10
8
34
1
2
3
4
Jakarta
Surabaya
Medan
Makassar
148
2002-09-30
45
1
2
3
4
5
Item
No.
1
2
3
4
5
6
7
item table
Item
Category
No.
T-Shirt
Trousers
Suites
Necktie
Ear Ring
Mesh Bag
Button
1
1
1
2
2
2
2
Unit
Price
Cost
Unit
Profit
39,800
78,000
345,000
54,000
19,800
29,800
7,000
29,000
56,000
198,000
47,000
11,000
13,000
5,000
10,800
22,000
147,000
7,000
8,800
16,800
2,000
category
table
Category
No.
1
2
Category
Clothes
Accessory
Well, you can do it by just giving appropriate SQL command to the same function
Caps Lock
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Date
Branch
Item
No.
Qty
2002-07-01
2002-07-01
2002-07-02
2002-07-03
2002-07-04
Jakarta
Medan
Surabaya
Jakarta
Makassar
2
3
6
3
2
12
4
10
8
34
45
148
2002-09-30
Jakarta
Then, the complete SQL command to produce exactly the same output with 2.2 will
be as follows
SELECT `No.`, Date, Branch, Item, Category,
`Unit Price`, Qty,
`Unit Price` * Qty as Sales,
`Unit Profit` * Qty as Profit
FROM sales2, branch, item, category
WHERE (sales2.`Item No.` = item.`Item No.`)
AND (sales2.`Branch No.`= branch.`Branch No.`)
AND (item.`Category No.`= category.`Category No.`)
ORDER BY `No.`
Calculate values
for output and
give them name
Total Sales
82,183,200
59,208,000
41,242,200
83,692,400
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Average Qty
Button
Ear Ring
Mesh Bag
Necktie
Suites
T-Shirt
Trousers
13
62
19
47
7
58
23
Sex
Birthday
M
F
M
M
M
1961-07-01
1987-09-23
1989-11-25
2002-07-03
2002-07-04
Address
Telephone
02-1234-0987
02-2341-9573
02-0492-9713
03-6731-1231
04-4827-3719
<HTML><BODY>
<H1>Search in participants database</H1>
<FORM method="POST">
<p>Name:<INPUT type="text" name="name"></p>
<p>Sex: <INPUT type="radio" name="sex" value="M">Male
<INPUT type="radio" name="sex" value="F">Female</p>
<p><INPUT type="submit" name="submit" value="OK"></p>
<p><INPUT type="reset" name="reset" value="Clear"></p>
</FORM>
<HR>
<?php
Function to report error
function invalid($error_message) {
echo "<p><font color='red'>$error_message</font></p>";
echo "</BODY></HTML>";
die;
}
require_once("show_MySQL_table.inc");
if (isset($_POST["submit"])) {
$sql = "SELECT * FROM participants WHERE";
$option = "";
if (isset($_POST["name"])) {
Form
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
$name = $_POST["name"];
if (strlen($name) > 50) invalid("Name is too long");
if ($name != "") $option .= " Name LIKE '%$name%'";
}
if (isset($_POST["sex"])) {
if ($option != "") $option .= " AND";
$option .= " Sex = '".$_POST["sex"]."'";
}
if ($option != "") {
$sql .= $option;
echo "<p>", htmlspecialchars($sql), "</p>";
show_MySQL_table("localhost","yoichi","","test",$sql);
}
}
?>
</BODY></HTML>
Useful data validation functions
isset($var)
echo isset($var);
$var = "Hello!";
echo isset($var);
is_numeric($var)
$var = "Hello!";
echo is_numeric($var); // This will show nothing
$var = "1234";
echo is_numeric($var); // Now this will show 1 (TRUE)
checkdate($month,$day,$year)
echo checkdate(2,31,2003);
echo checkdate(2,28,2003);
10
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Strip all HTML and PHP tags from the specified string. Useful
for message board system to prevent abuse.
11