<?
ob_start();
echo <<< BEGIN
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>
	Student Homepages
  </TITLE>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  <META HTTP-EQUIV="Expires" CONTENT="-1">
 </HEAD>
 <BODY OnLoad="document.MyForm.Name.focus(); document.MyForm.reset();">
 <H1 ALIGN="center">Student Homepages</H1>
BEGIN;

function refresh()
{
	header('Refresh:0');	
	echo "<center><font color='red'>Thanks for signining up.</font></center>\n";
}
@$d = mysql_connect('localhost','abu-jeib','iyad911') or  die("<p>We can't process your request at this time.</p>");

@mysql_select_db('exams') OR die ("<p>We can't process your request at this time.</p>");


// Display Records
echo "<center><table width='60%'><tr><td><ol>\n";

@$q = "select * from exam2 order by lower(last), lower(first_middle)";
@$r = mysql_query($q) OR die ("<p>We can't process your request at this time.</p>");
while (@$row = mysql_fetch_array($r))
{
        echo "<li><a href='$row[url]'>".ucwords(strtolower($row[first_middle]))." ".ucwords(strtolower($row[last]))."</a>";
        echo "(<I>Created on $row[date]</I>)</li>\n";
	// You could also use:
	/*
	echo "<li><a href='$row[2]'>".ucwords(strtolower($row[1]))." ".ucwords(strtolower($row[0]))."</a>";
        echo "(<I>Created on $row[3]</I>)</li>\n";
	*/
}

echo "</ol><hr color='black' height='3'/>\n";

mysql_free_result($r); // Write it if concerned about memory. 
			// No need to put it if it's in the end
			// (done automatically there).

// Process data submitted by form (to add a homepage to the database)
if (isset($_POST['submitted']))
{
 $last = trim($_POST['last']);
 $first = trim($_POST['first']);
 $middle = trim($_POST['middle']);
 $url = trim($_POST['url']);

 $errors = array();
 if (empty($last)) // Last name should not be empty
	$errors[] = "Your last name.";
 if (empty($first)) // First name should not be empty
	$errors[] = "Your first name.";
 if (empty($url)) // URL should not be empty
 	$errors[] = "The URL of your homepage."; 
 if (empty($errors))
 {
	// Find if http:// is a part of URL.
	// If not, add it.
	$pos = stripos($url,"http://");
	if ($pos === false && !empty($url)) // Don't use == here
		$url = "http://".$url;
	$first_middle = "$first $middle";
	
	// Check if the name is already in the list.
	// If not, don't add it.
	@$q = 'select count(*) from exam2 where last="$last" and first_middle="$first_middle"'; 
	@$r = mysql_query($q) OR die ("<p>We can't process your request at this time.</p>");
	$row = mysql_fetch_row($r); // Could use $row = mysql_fetch_array($r);
	if ($row[0] > 0) // If name exists
		echo "<p align='center'>You're already signed up.</p>";
	else // If name not there, add record to database
	{
		$q = "insert into exam2 values ('$last','$first_middle','$url',now())";
		@$r = mysql_query($q) OR die ("<p>We can't process your request at this time.</p>");
		#echo "<font color='red'><center>Thanks for signing up.</center></font>";
		refresh(); // To make new added homepage appear on website
	}	
 }
 else // If last name or first name or URl left empty
 {
	echo "<h2 align=\"center\">Missing Information</h2><center>\n";
	echo "Click the 'back' button in the browser, and fill out:<br> \n";
	echo "<FONT COLOR='red'>";
	foreach ($errors as $error)
		echo "$error<br>\n";
	echo "</FONT></center>\n";
 } 
} // if submitted
else // Display form
{
	echo <<< BEGIN
	<CENTER>
	<h3>To join the list, please fill out the following form:</h3>
	<TABLE WIDTH="60%">
	 <TR>
	  <TD>
	   <FORM name="MyForm" ACTION="{$_SERVER['PHP_SELF']}" METHOD="POST">
		<B>*Last Name:</B> <INPUT NAME="last" SIZE="30" TYPE="text" STYLE="width:100%;">
		<B>*First Name:</B> <INPUT NAME="first" SIZE="30" TYPE="text" STYLE="width:100%;">
		<B>Middle Name or Middle Initial:</B> <INPUT NAME="middle" SIZE="30" TYPE="text" STYLE="width:100%;">
		<B>*Homepage:</B> <INPUT NAME="url" SIZE="30" TYPE="text" STYLE="width:100%;">
		<INPUT NAME="submitted" TYPE="hidden" VALUE="TRUE">
		<p></p>
		<CENTER>
		 <INPUT TYPE="Submit" VALUE="SUBMIT"> 
		 <INPUT TYPE="reset" VALUE="CLEAR">
		</CENTER>
	  </FORM>
	  <center><I>The fields marked with * must be filled out.</I></center>
	</td>
      </tr>
     </table>
BEGIN;
}
@mysql_close($d) OR die();
ob_end_flush();
?>

 </center>
 </body>
</html>

