|
Web Design ![]() This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Lecture /
Case study: the functions of database, server-side scripts, and client-side scriptsA Web Design Lecture by Steven Choy Overview: In this lecture, we will go through all the necessary steps in building a website which uses MySQL (database) to store information, PHP (server-side script) to retrieve and update information in database, JavaScript (client-side script) to asynchronously send request to the PHP program. You will understand more about the functions of database, server-side scripts, and client-side scripts in building a website. You will also be introduced what is AJAX. Introduction: the end product
![]() (The final website we are going to build) Step 1: HTML for contents
step1.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>ITS234F 2009 Assignment One Submission</title> </head> <body> <h1>ITS234F 2009 Web Design Assignment One</h1> <p>The following are web design works submitted by your fellows. Please show your support and appreciation.</p> <ol> <li><a href="/its234f2009/a1/s556676x">Chan Pui Cheung</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559066x">Chow Man Wai</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559098x">Lee Man Yi</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559099x">Lee Tat Ming</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559133x">Lee Cheuk Kin</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559136x">Po Chi Kai</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559273x">Ng Pui Yan</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559330x">Wong Chok Pang</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559561x">Wong Yiu Wai</a> (<a href="#">Like</a>: 1)</li> <li><a href="/its234f2009/a1/s559916x">Yu Suet Yee</a> (<a href="#">Like</a>: 1)</li> </ol> <p>Please let Steven konw if there are any problems.</p> </body> </html> ![]() (The website screen shot after Step 1) Step 2: CSS for presentation
step2.css ol {list-style: none; line-height:200% } li { background:url(note.png) center left no-repeat; padding:0 0 0 20px } .counter { background:url(heart.png) center left no-repeat; padding:0 0 0 20px; font-weight:bold; margin-left:10px; } .counter a { cursor:pointer; } .counter a.image { background:url(add.png) center right no-repeat; padding:0 20px 0 0; } step2.html
![]() (The website screen shot after Step 2) Step 3: MySQL for database
CREATE DATABASE `its234f` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `its234f`.`liked` (
`ID` varchar(20) NOT NULL,
`name` varchar(50) default NULL,
`link` varchar(256) default NULL,
`likedCount` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
![]() (You can import data from an external source to MySQL)
s556676x;Chan Pui Cheung;/its234f2009/a1/s556676x;0
s559066x;Chow Man Wai;/its234f2009/a1/s559066x;0
s559098x;Lee Man Yi;/its234f2009/a1/s559098x;0
s559099x;Lee Tat Ming;/its234f2009/a1/s559099x;0
s559133x;Lee Cheuk Kin;/its234f2009/a1/s559133x;0
s559136x;Po Chi Kai;/its234f2009/a1/s559136x;0
s559273x;Ng Pui Yan;/its234f2009/a1/s559273x;0
s559330x;Wong Chok Pang;/its234f2009/a1/s559330x;0
s559561x;Wong Yiu Wai;/its234f2009/a1/s559561x;0
s559916x;Yu Suet Yee;/its234f2009/a1/s559916x;0
INSERT INTO `liked` (`ID`, `name`, `link`, `likedCount`) VALUES
('s556676x', 'Chan Pui Cheung', '/its234f2009/a1/s556676x', 0),
('s559066x', 'Chow Man Wai', '/its234f2009/a1/s559066x', 0),
('s559098x', 'Lee Man Yi', '/its234f2009/a1/s559098x', 0),
('s559099x', 'Lee Tat Ming', '/its234f2009/a1/s559099x', 0),
('s559133x', 'Lee Cheuk Kin', '/its234f2009/a1/s559133x', 0),
('s559136x', 'Po Chi Kai', '/its234f2009/a1/s559136x', 0),
('s559273x', 'Ng Pui Yan', '/its234f2009/a1/s559273x', 0),
('s559330x', 'Wong Chok Pang', '/its234f2009/a1/s559330x', 0),
('s559561x', 'Wong Yiu Wai', '/its234f2009/a1/s559561x', 0),
('s559916x', 'Yu Suet Yee', '/its234f2009/a1/s559916x', 0);
Step 4: PHP for interaction with MySQLStep 4.a: HTML and PHP script to retrieve data from database
<?php
$dbhost = 'localhost'; //usually localhost
$dbuser = 'root'; //your username assigned to your database
$dbpass = 'yourdbpass'; //your password assigned to your user & database
$dbname = 'yourdbname'; //your database name
?>
<?php
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
$q = 'SELECT * FROM liked';
$res = mysql_query($q);
mysql_close($conn);
$i=0;
while($row = mysql_fetch_array($res)) {
$item[$i]['link'] = $row['link'];
$item[$i]['name'] = $row['name'];
$item[$i]['likedCount'] = $row['likedCount'];
$item[$i]['ID'] = $row['ID'];
$i = $i + 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>ITS234F 2009 Assignment One Submission</title>
<link href="step2.css" type="text/css" rel="stylesheet" media="screen"/>
</head>
<body>
<h1>ITS234F 2009 Web Design Assignment One</h1>
<p>The following are web design works submitted by your fellows.
Please show your support and appreciation.</p>
<ol>
<?php foreach ($item as $output) { ?>
<li>
<a href="<?php echo $output['link']; ?>"><?php echo $output['name']; ?></a>
<span class="counter">
<a href="#" class="image"><?php echo $output['likedCount']; ?></a>
</span>
</li>
<?php } ?>
</ol>
<p>Please let Steven konw if there are any problems.</p>
</body>
</html>
Step 4.b: PHP script to update data to database
<?php
$id = $_GET['id'];
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
if($id != '') {
$update = "update liked set likedCount = likedCount + 1 WHERE ID = '".$id."'";
$result = mysql_query($update);
$sql= "select * from liked WHERE ID= '".$id."' LIMIT 0, 1";
$result = mysql_query($sql);
$rs= mysql_fetch_array($result);
echo $rs[likedCount];
}
?>
Step 4.c: Call the script with a HTTP GET method
<?php
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
$q = 'SELECT * FROM liked';
$res = mysql_query($q);
mysql_close($conn);
$i=0;
while($row = mysql_fetch_array($res)) {
$item[$i]['link'] = $row['link'];
$item[$i]['name'] = $row['name'];
$item[$i]['likedCount'] = $row['likedCount'];
$item[$i]['ID'] = $row['ID'];
$i = $i + 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>ITS234F 2009 Assignment One Submission</title>
<link href="step2.css" type="text/css" rel="stylesheet" media="screen"/>
</head>
<body>
<h1>ITS234F 2009 Web Design Assignment One</h1>
<p>The following are web design works submitted by your fellows.
Please show your support and appreciation.</p>
<ol>
<?php foreach ($item as $output) { ?>
<li>
<a href="<?php echo $output['link']; ?>"><?php echo $output['name']; ?></a>
<span class="counter">
<a href="step4b.php?id=<?php echo $output['ID'] ?>" class="image">
<?php echo $output['likedCount']; ?>
</a>
</span>
</li>
<?php } ?>
</ol>
<p>Please let Steven konw if there are any problems.</p>
</body>
</html>
Step 4.d: Use Cookie to limit the user interaction
<?php
$id = $_GET['id'];
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
if($id != '') {
if (!isset($_COOKIE['liked-'.$id])) {
$update = "update liked set likedCount = likedCount + 1 WHERE ID = '".$id."'";
$result = mysql_query($update);
$sql= "select * from liked WHERE ID= '".$id."' LIMIT 0, 1";
$result = mysql_query($sql);
$rs= mysql_fetch_array($result);
setcookie('liked-'.$id, time(), time()+3600*24*365, '/');
echo $rs[likedCount];
} else {
echo 'You have voted';
}
}
?>
Step 4.3: Use HTTP POST method to further limit the user interaction
<?php
$id = $_POST['id'];
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
if($id != '') {
if (!isset($_COOKIE['liked-'.$id])) {
$update = "update liked set likedCount = likedCount + 1 WHERE ID = '".$id."'";
$result = mysql_query($update);
$sql= "select * from liked WHERE ID= '".$id."' LIMIT 0, 1";
$result = mysql_query($sql);
$rs= mysql_fetch_array($result);
setcookie('liked-'.$id, time(), time()+3600*24*365, '/');
echo $rs[likedCount];
} else {
echo 'You have voted';
}
}
?>
Step 5: AJAX to make asynchronous request to server
<?php
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
$q = 'SELECT * FROM liked';
$res = mysql_query($q);
mysql_close($conn);
$i=0;
while($row = mysql_fetch_array($res)) {
$item[$i]['link'] = $row['link'];
$item[$i]['name'] = $row['name'];
$item[$i]['likedCount'] = $row['likedCount'];
$item[$i]['ID'] = $row['ID'];
$i = $i + 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>ITS234F 2009 Assignment One Submission</title>
<link href="step2.css" type="text/css" rel="stylesheet" media="screen"/>
</head>
<body>
<h1>ITS234F 2009 Web Design Assignment One</h1>
<p>The following are web design works submitted by your fellows.
Please show your support and appreciation.</p>
<ol>
<?php foreach ($item as $output) { ?>
<li>
<a href="<?php echo $output['link']; ?>"><?php echo $output['name']; ?></a>
<span class="counter">
<a onclick="likeThis('<?php echo $output['ID']; ?>');" class="image">
<?php echo $output['likedCount']; ?>
</a>
</span>
</li>
<?php } ?>
</ol>
<p>Please let Steven konw if there are any problems.</p>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
function likeThis(postId) {
if (postId != '') {
jQuery('#iLikeThis-postId .counter').text('...');
jQuery.post("step4e.php",
{ id: postId },
function(data){
jQuery('#iLikeThis-postId .counter').text(data);
});
}
}
</script>
</body>
</html>
step5b.css ol {list-style: none; line-height:200% } li { background:url(note.png) center left no-repeat; padding:0 0 0 20px } .iLikeThis { display:inline; } .iLikeThis .counter { background:url(heart.png) center left no-repeat; padding:0 0 0 20px; font-weight:bold; margin-left:10px; } .iLikeThis .counter a { cursor:pointer; } .iLikeThis .counter a.image { background:url(add.png) center right no-repeat; padding:0 20px 0 0; }
<?php
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
$q = 'SELECT * FROM liked';
$res = mysql_query($q);
mysql_close($conn);
$i=0;
while($row = mysql_fetch_array($res)) {
$item[$i]['link'] = $row['link'];
$item[$i]['name'] = $row['name'];
$item[$i]['likedCount'] = $row['likedCount'];
$item[$i]['ID'] = $row['ID'];
$i = $i + 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>ITS234F 2009 Assignment One Submission</title>
<link href="step5b.css" type="text/css" rel="stylesheet" media="screen"/>
</head>
<body>
<h1>ITS234F 2009 Web Design Assignment One</h1>
<p>The following are web design works submitted by your fellows.
Please show your support and appreciation.</p>
<ol>
<?php foreach ($item as $output) { ?>
<li>
<a href="<?php echo $output['link']; ?>"><?php echo $output['name']; ?></a>
<div id="iLikeThis-<?php echo $output['ID']; ?>" class="iLikeThis">
<span class="counter">
<a onclick="likeThis('<?php echo $output['ID']; ?>');" class="image">
<?php echo $output['likedCount']; ?>
</a>
</span>
</div>
</li>
<?php } ?>
</ol>
<p>Please let Steven konw if there are any problems.</p>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
function likeThis(postId) {
if (postId != '') {
jQuery('#iLikeThis-postId .counter').text('...');
jQuery.post("step4e.php",
{ id: postId },
function(data){
jQuery('#iLikeThis-postId .counter').text(data);
});
}
}
</script>
</body>
</html>
<?php
include ('db.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
$q = 'SELECT * FROM liked';
$res = mysql_query($q);
mysql_close($conn);
$i=0;
while($row = mysql_fetch_array($res)) {
$item[$i]['link'] = $row['link'];
$item[$i]['name'] = $row['name'];
$item[$i]['likedCount'] = $row['likedCount'];
$item[$i]['ID'] = $row['ID'];
$i = $i + 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>ITS234F 2009 Assignment One Submission</title>
<link href="step5b.css" type="text/css" rel="stylesheet" media="screen"/>
</head>
<body>
<h1>ITS234F 2009 Web Design Assignment One</h1>
<p>The following are web design works submitted by your fellows.
Please show your support and appreciation.</p>
<ol>
<?php foreach ($item as $output) { ?>
<li>
<a href="<?php echo $output['link']; ?>"><?php echo $output['name']; ?></a>
<div id="iLikeThis-<?php echo $output['ID']; ?>" class="iLikeThis">
<span class="counter">
<?php if (!isset($_COOKIE['liked-'.$output['ID']])) { ?>
<a onclick="likeThis('<?php echo $output['ID']; ?>');" class="image">
<?php echo $output['likedCount']; ?>
</a>
<?php } else { ?>
<?php echo $output['likedCount']; ?>
<?php } ?>
</span>
</div>
</li>
<?php } ?>
</ol>
<p>Please let Steven konw if there are any problems.</p>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
function likeThis(postId) {
if (postId != '') {
jQuery('#iLikeThis-postId .counter').text('...');
jQuery.post("step4e.php",
{ id: postId },
function(data){
jQuery('#iLikeThis-postId .counter').text(data);
});
}
}
</script>
</body>
</html>
What is AJAX and how does it work?
GET and POST Method of HTTP
"The Get is one the simplest Http method. Its main job is to ask the server for the resource. If the resource is available then then it will given back to the user on your browser. That resource may be a HTML page, a sound file, a picture file (JPEG) etc. We can say that get method is for getting something from the server. It doesn't mean that you can't send parameters to the server. But the total amount of characters in a GET is really limited. In get method the data we send get appended to the URL so whatever you will send will be seen by other user so can say that it is not even secure."
"The Post method is more powerful request. By using Post we can request as well as send some data to the server. We use post method when we have to send a big chunk of data to the server, like when we have to send a long enquiry form then we can send it by using the post method."
Thanks for ReadingIf you would rather like to have this lecture note in printed format, please click the print action link in the top right corner. If you find any problem in this lecture note, please feel free to tell Steven via steven [at] findaway.hk. |