Professional Documents
Culture Documents
newuser.php................................................................................................................................................1
php_newuser.php........................................................................................................................................3
login.php....................................................................................................................................................3
php_login.php............................................................................................................................................4
php_logout.php..........................................................................................................................................5
editinfo.php................................................................................................................................................5
php_editinfo.php........................................................................................................................................9
search.php..................................................................................................................................................9
php_search.php........................................................................................................................................10
livesearch.js..............................................................................................................................................11
php_livesearch.js......................................................................................................................................13
autorefresh.php.........................................................................................................................................14
userlist.php...............................................................................................................................................14
index.php..................................................................................................................................................15
newuser.php
<?php include('index.php'); ?>
<html>
<head>
<title>Add New User</title>
</head>
<body bgcolor="#82cafa">
<center>
<br>
<table border=1>
<form action="php_newuser.php" method="POST">
<tr>
<td width="30%">* Username</td><td width="50%"><input type="text"
name="username" maxlength="10" size='28'/></td>
</tr>
<tr>
<td width="30%">* Password</td><td width="50%"><input type="password"
name="userpass" maxlength="10" size='28'/></td>
</tr>
<tr>
<td width="30%">* Last Name</td><td width="50%"><input type="text"
name="lastname" maxlength="25" size='28'/></td>
</tr>
<tr>
<td width="30%">* First Name</td><td width="50%"><input type="text"
name="firstname" maxlength="25" size='28'/></td>
</tr>
<tr>
<td width="30%">* Job Title</td>
<td width="50%">
<select name="jobtitle">
<option value="MIS">MIS</option>
<option value="TIO">TIO</option>
<option value="PRO">PRO</option>
<option value="CSO">CSO</option>
</select>
</td>
</tr>
<tr>
<td width="30%">* Birthday</td>
<td width="50%">
<select name="b_month">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
1
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="b_day">
<?php
for ($counter = 1; $counter <= 31; $counter++)
{
echo "<option value='$counter'>$counter</option>";
}
?>
</select>
<select name="b_year">
<?php
$time = time();
$year = date('Y',$time);
for($x=$year-18; $x>=$year-100; $x--)
{
echo "<option value='$x'>$x</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td width="30%">* Mobile</td>
<td width="50%"><input type="text" name="mobile" maxlength="25"
size='28'/></td>
</tr>
<tr>
<td width="30%">* Phone</td>
<td width="50%"><input type="text" name="phone" maxlength="25"
size='28'/></td>
</tr>
<tr>
<td width="30%">* Email</td>
<td width="50%"><input type="text" name="email" maxlength="50"
size='28'/></td>
</tr>
<tr>
<td width="30%">* Address</td>
<td width="50%"><textarea name="address" rows="2"
cols="22"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Add New User"></td>
</tr>
</form>
</table>
</center>
</body>
</html>
2
php_newuser.php
<?php
//create query
$query = "INSERT INTO users(username, userpass, lastname, firstname, jobtitle,
bday, mobile, phone, email, address)
VALUES('$username','$userpass','$lastname','$firstname',
'$jobtitle', '$bday', '$mobile', '$phone', '$email',
'$address')";
//execute query
$result = mysql_query($query)
or die ("Error in query: $query. " . mysql_error());
--------------------------------------------------------------------------------
login.php
<?php include('index.php'); ?>
<html>
<head>
<title>Home</title>
</head>
<body bgcolor="#82cafa">
3
<tr>
<td colspan="2" align="center">USER LOG IN</td>
</tr>
<tr>
<td>UserName</td>
<td><input type="text" name="username" size="10" maxlength="10"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="userpass" size="10" maxlength="10"/></td>
</tr>
<tr>
<td> </td><td align="center"><input type="submit" name="submit"
value="Log In"/></td>
</tr>
<tr><td> </td><td> </td></tr>
<tr>
<td colspan="2" align="center">New user? Click <a
href="newuser.php">here</a>.</td>
</tr>
</table>
</form>
</body>
</html>
--------------------------------------------------------------------------------
php_login.php
<?php
if(empty($_SESSION['userID']))
{ session_start(); }
$username = $_POST['username'];
$userpass = $_POST['userpass'];
$query = "SELECT userid FROM users WHERE username = '$username' AND userpass =
'$userpass'";
$result = mysql_query($query)
or die ("Error in query: ".mysql_error());
$row = mysql_fetch_object($result);
4
if (mysql_num_rows($result)> 0)
{
$_SESSION['userID']=$row->userid;
header("Location: editinfo.php");
}
else
{
echo '<h2>Login Failed!</h2>';
}
--------------------------------------------------------------------------------
php_logout.php
<?php
session_start();
session_destroy();
header("Location: index.php");
?>
--------------------------------------------------------------------------------
editinfo.php
<?php
include('index.php');
?>
<html>
<head>
<title>Edit User Information</title>
</head>
<body bgcolor="#82cafa">
<?php
$userID =$_SESSION['userID'];
$result = mysql_query($query)
5
or die ("Error in query: ".mysql_error());
$row = mysql_fetch_object($result);
if (mysql_num_rows($result)>0)
{
?>
<center>
<br>
<form action="php_editinfo.php" method="POST">
<table border=1>
<tbody>
<tr>
<td width="30%">* Last Name</td><td width="50%"><input type="text"
name="lastname" value=<?php echo "$row->lastname"; ?> maxlength="25"
size='28'/></td>
</tr>
<tr>
<td width="30%">* First Name</td><td width="50%"><input type="text"
name="firstname" value=<?php echo "$row->firstname"; ?> maxlength="25"
size='28'/></td>
</tr>
<tr>
<td width="30%">* Job Title</td>
<td width="50%">
<?php
$titles = array("MIS","TIO","PRO","CSO");
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td width="30%">* Birthday</td>
<td width="50%">
<select name="b_month">
<?php
$months = array('01'=>'January','02'=>'February',
'03'=>'March','04'=>'April','05'=>'May',
'06'=>'June','07'=>'July','08'=>'August',
'09'=>'September','10'=>'October',
'11'=>'November','12'=>'December');
$bday = explode('-',$row->bday);
foreach ($months as $num => $month )
{
echo "<option ";
if ($num == $bday[1])
6
{ echo "selected='selected'"; }
echo "value=$num>$month</option>";
}
echo "</select>";
?>
</select>
<select name="b_day">
<?php
for ($counter = 1; $counter <= 31; $counter++)
{
echo "<option ";
if ($counter == $bday[2])
{ echo "selected='selected'"; }
echo "value='$counter'>$counter</option>";
}
?>
</select>
<select name="b_year">
<?php
$time = time();
$year = date('Y',$time);
for($x=$year-18; $x>=$year-100; $x--)
{
echo "<option ";
if ($x == $bday[0])
{ echo "selected='selected'"; }
echo "value='$x'>$x</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td width="30%">* Mobile</td>
<td width="50%">
<?php
if (empty($row->mobile))
{
echo "<input type='text' name='mobile' maxlength='25' size='28'/>";
}
else
{
echo "<input type='text' name='mobile' value=$row->mobile
maxlength='25' size='28'/>";
}
?>
</td>
</tr>
<tr>
<td width="30%">* Phone</td>
<td width="50%">
<?php
if (empty($row->phone))
{
echo "<input type='text' name='phone' maxlength='25' size='28'/>";
}
7
else
{
echo "<input type='text' name='phone' value=$row->phone
maxlength='25' size='28'/>";
}
?>
</td>
</tr>
<tr>
<td width="30%">* Email</td>
<td width="50%">
<?php
if (empty($row->email))
{
echo "<input type='text' name='email' maxlength='25' size='28'/>";
}
else
{
echo "<input type='text' name='email' value=$row->email
maxlength='50' size='28'/>";
}
?>
</td>
</tr>
<tr>
<td width="30%">* Address</td>
<td width="50%"><textarea name="address" rows="2" cols="22"><?php echo
"$row->address"; ?></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Edit Information"></td>
</tr>
</tbody>
</table>
</form>
</center>
<?php
}
else
{
echo "You must login first. <br> Click <a href='login.php'>here</a> to
login.";
echo $userID.$_SESSION['userID'];
}
?>
</body>
</html>
--------------------------------------------------------------------------------
8
php_editinfo.php
<?php
session_start();
//connect to mysql database
$con = mysql_connect("localhost","root","admin")
or die('Unable to connect: ' . mysql_error());
//create query
$query = "UPDATE users SET lastname='$lastname', firstname='$firstname',
jobtitle='$jobtitle', bday='$bday', mobile='$mobile',
phone='$phone',
email='$email', address='$address' WHERE userID = '$userID'";
//execute query
$result = mysql_query($query)
or die ("Error in query: $query. " . mysql_error());
--------------------------------------------------------------------------------
search.php
<?php
include('index.php');
?>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="livesearch.js"></script>
<style type="text/css">
9
.suggest_link
{
background-color: #FFFFFF;
padding: 2px 6px 2px 6px;
}
.suggest_link_over
{
background-color: #E0FFFF;
padding: 2px 6px 2px 6px;
}
#livesearch
{
margin:0px;
width:215px;
}
#txt1
{
margin:0px;
}
</style>
<title></title>
</head>
<body>
<form action="php_search.php" method="GET">
<center>
<table>
<tr>
<td valign="top">Search:</td>
<td><input type="text" name="searchkey" id="txt1"
size="30" autocomplete="off" onkeyup="showResult(this.value)" />
<div id="livesearch"></div>
</td>
<td valign="top"><input type="submit" value="Go"
name="go" /></td>
</tr>
</table>
</center>
</form>
</body>
</html>
--------------------------------------------------------------------------------
php_search.php
<?php
include ('search.php');
$searchkey = $_GET['searchkey'];
$name = explode(",",$searchkey);
10
firstname='$name[1]'";
$result = mysql_query($query)
or die ("Error in query: ".mysql_error());
$row = mysql_fetch_object($result);
$rec = mysql_num_rows($result);
echo "
<center>
<table border='1'>
<thead>
<tr>
<th>User ID</th>
<th>Name</th>
<th>Birthday</th>
<th>Mobile</th>
<th>Phone</th>
<th>Email</th>
</tr>
</thead>
<tbody>";
for($ctr=1; $ctr<=$rec;$ctr++)
{
echo "<tr>
<td>$row->userid</td>
<td>$row->firstname $row->lastname</td>
<td>$row->bday</td>
<td>$row->mobile</td>
<td>$row->phone</td>
<td>$row->email</td>
</tr>";
}
echo"</tbody>
</table>
</center>";
?>
--------------------------------------------------------------------------------
livesearch.js
var xmlhttp;
function showResult(str)
{
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
xmlhttp=GetXmlHttpObject()
if (xmlhttp==null)
{
alert ("Your browser does not support XML HTTP Request");
return;
11
}
var url="php_livesearch.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged ;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
var doc = document.getElementById("livesearch")
doc.innerHTML='';
str=xmlhttp.responseText.split("\n");
//Click function
function setSearch(value)
{
document.getElementById('txt1').value = value;
document.getElementById('livesearch').innerHTML = '';
document.getElementById("livesearch").style.border="0px";
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
12
}
return null;
}
--------------------------------------------------------------------------------
php_livesearch.js
<?php
$con = mysql_connect("localhost", "root", "admin");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("calendar",$con);
$hint="";
while($row = mysql_fetch_array($result))
{
if ($hint=="")
{
$hint = $row['suggest'].", ".$row['firstname']."\n";
}
else
{
$hint = $hint.$row['suggest'].", ".$row['firstname']."\n";
}
}
--------------------------------------------------------------------------------
13
autorefresh.php
<?php
include ('index.php');
?>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function refresh()
{
document.all.iframe1.src = document.all.iframe1.src;
}
window.setInterval("refresh()",3000);
</script>
--------------------------------------------------------------------------------
userlist.php
<?php
//header("Refresh: 5;login.php");
//include('index.php');
?>
<html>
<head>
<!-- <META HTTP-EQUIV="refresh" CONTENT="2; URL="> -->
<!--<META HTTP-EQUIV="Refresh" CONTENT="5; URL=$_SERVER['PHP_SELF']">-->
<title>User List</title>
</head>
<body bgcolor="#82cafa">
<?php
//connect to mysql database
$con = mysql_connect("localhost","root","admin")
or die('Unable to connect: ' . mysql_error());
$result = mysql_query($query)
or die ("Error in query: ".mysql_error());
14
if (mysql_num_rows($result)>0)
{
?>
<center>
<br>
<table border=1>
<tr>
<td width="30%" align="center">List of Users</td>
</tr>
<?php
while($row=mysql_fetch_row($result))
{
if (!empty($row[0]))
{
$name = ucfirst($row[0]).", ".ucfirst($row[1]);
echo "<tr><td>$name</td></tr>";
}
}
?>
</table>
</center>
<?php
}
else
{
echo "No users found.";
}
?>
</body>
</html>
--------------------------------------------------------------------------------
index.php
<?php
if(empty($_SESSION['userID']))
{ session_start(); }
?>
<html>
<head><title></title>
</head>
<body bgcolor="#82cafa">
<center>
<a href="index.php">Home</a> |
<?php
if (empty($_SESSION['userID']))
{
echo "<a href='login.php'>Log In</a> | ";
echo "<a href='newuser.php'>Add New User</a> | ";
}
else
{
15
echo "<a href='php_logout.php'>Log Out</a> | ";
echo "<a href='editinfo.php'>Edit Information</a> | ";
}
?>
<a href="autorefresh.php">Users</a> |
<a href="calendar.php"> Calendar</a> |
<a href="search.php">Search</a>
<br>
<br>
<hr>
</center>
</body>
</html>
16