<?php
$dir = 'advanced';
$doc = 'email.txt';
$file = "$dir/$doc";
/**********************************
Delete
**********************************/
if(isset($_POST['deleteButton'])){
$toDelete = $_POST['toDelete'][0];
// Get the contents
$file_handle = fopen($file , 'r');// Read file
while(!feof($file_handle)){
$file_contents[] = fgets($file_handle);
}
fclose($file_handle);
// Find a match
// Purge out the line to be deleted
// And reset the array to be rewritten
foreach($file_contents as $key => $line){
$line = trim($line);
if($toDelete == $line){
$file_contents[$key] = '';
}
}
//Write file contents back into the text file
$file_handle = fopen($file, 'w');
foreach($file_contents as $line){
fwrite($file_handle, $line);
}
fclose($file_handle);
}
/**********************************
Add lines to the file
**********************************/
if(isset($_POST['submit'])){
if(($_POST['name'] != '') && ($_POST['email'] != '')){
if(preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i" , $_POST['email'])){
if(is_file($file)){
// Write to file
$file_handle = fopen("$file" , 'a'); //Append to file
fputs($file_handle, $_POST['name'] . "¥" . $_POST['email'] . "\r\n");
fclose($file_handle);
header("Location: $PHP_SELF");
} else {
$message = 'Cannot Write to file. File does not exist';
}
} else {
$message = 'Invalid Email Address';
}
} else {
$message = 'Nothing to enter BASTAAARD!!!';
}
} else {
$message = "<br />";
}
?>
<html>
<head>
<title>Week #10 Advanced ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>
<?php
if($message){
print("$message");
}
?>
<form method="POST" action="<?php $PHP_SELF ?>" style="float:left;">
<p>Name: <input type ="text" name="name"></p>
<p>Email: <input type="text" name="email"></p>
<p><input type="submit" name="submit" value="submit"></p>
</form>
<br /><br />
<form method="POST" action="<?php $PHP_SELF ?>">
<input type="hidden" name="action" value="delete">
<table border="1" style="border-collapse:collapse;line-height:8px;padding:0px;margin-left:10px;">
<tr><td><b>Name</b></td><td><b>Email</b></td></tr>
<?php
// Read and display the file contents
if(is_file($file)){
// Get the contents
$file_handle = fopen("$file" , 'r');// Read file
while(!feof($file_handle)){
$file_contents[] = fgets($file_handle);
}
fclose($file_handle);
// Show Contents
foreach($file_contents as $key => $value){
$row_data = explode("¥" , $value);
if($value != ''){
print("<form method=\"POST\" action=\"$PHP_SELF\">");
print("<input type=\"hidden\" name=\"toDelete[]\" value=\"$value\">");
print("<tr><td>$row_data[0]</td><td>$row_data[1]</td><td><input type=\"submit\" name=\"deleteButton\" value=\"Delete\" /></td></tr>");
print("</form>");
}
}
} else {
print("There's a problem reading the file");
}
?>
</table></form><br clear="left"/>
<a href='../index.html'>BACK</a>
<br>
<iframe src='wk10advsrc.php' width='100%' height='300'></iframe>
<br>
<a href='../index.html'>BACK</a>
</body>
</html>