<html>
<head>
<title>Week #10 Checkbox ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
</head>
<body>
<?php
$path_to_dir = 'basic'; // path to text file
// [ ADD ENTRY TO FLAT FILE ]
if($_POST['action'] == "true"){
if(($name == " " || $email == " ") || ($name == "" || $email == "")){ // Disallow blank or empty string entries
print("<b>Invalid Entry</b>, Please go back and check your entry");
}else{
if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $email)){ // proceed ONLY if email is valid format
if($filehandle = fopen("$path_to_dir/email.txt", "a")){ // Open the file
fputs($filehandle, "$name" . ":" . "$email\r\n"); // Append the contents
$ResultsOfAdd = "Added,<b> $name </b>with the address<b> $email </b>to the flat file"; // Print confirm
fclose($filehandle);
}else{
echo $error;
}
}else{
print("Sorry, <b>$email</b>, is not a correct email format,<br>Please go back and correct the address");
}
}
}
?>
<table>
<tr>
<td>
<?php
$path_to_dir = 'basic'; // path to text file
$file_contents = file("$path_to_dir/email.txt"); // the file
// [ DELETE ROUTINE ]
if($_POST['action'] == "delete"){ // delete triggered
$arrToDelete = $_POST['todelete']; // assign checked items array to var
if($arrToDelete != "") { // if the array isnt open proceed
foreach($arrToDelete as $key => $value) { // assign a key and value to each checked item
array_splice($file_contents, $key, 1, "");// assign null value to each checked item in file_contents array
print("Deleted <b>$value</b><br>");
}
$sizeof = count($file_contents); // count updated file_contents array
if($filehandle = fopen("$path_to_dir/email.txt", "w")){ // open the file for writing
for($y = 0; $y < $sizeof; $y++){ // loop through the new file_contents array
if($file_contents){
fputs($filehandle, $file_contents[$y]); // write the new file_contents array to the file
}
}
fclose($filehandle); // close the file
}
}
}
?>
<?php // [ READ & OUTPUT FILE CONTENTS ] ?>
<!-- The Form & Table -->
<form method="POST" action="<?php $PHP_SELF ?>" style="margin:0;">
<input type="hidden" name="action" value="delete">
<table border="1">
<tr>
<td class="spectable">Name:</td><td class="spectable">Email:</td><td class="spectable">Delete:</td>
</tr>
<?php
$file_contents = file("$path_to_dir/email.txt"); // the file
foreach($file_contents as $key=>$value){ // assign a key to each line in the file
if($file_contents[$key] != ""){
$entry = explode(":", $file_contents[$key]); // break out each delimeter
print("<tr> <td class=spectable>".$entry[0]."</td>
<td class=spectable>".$entry[1]."</td>
<td class=spectable><center><input type=checkbox name=todelete[$key] value=$value></center></td>
</tr>"); // giving key and value to each checked item
}
}
?>
<tr>
<td colspan="3" align="center" class="spectable"><input type="submit" value="Delete Entry"></td>
</tr>
</table>
</form>
</td>
<td valign="top">
<?php print("$ResultsOfAdd"); ?>
<!-- Add Entry Form -->
<form method="POST" action="<? $PHP_SELF ?>">
<input type="hidden" name="action" value="true">
<table border="1">
<tr>
<td class="spectable">Name:</td><td class="spectable"><input type="text" name="name"></td>
</tr>
<tr>
<td class="spectable">Email:</td><td class="spectable"><input type="text" name="email"></td>
</tr>
<td colspan="2" class="spectable"><center><input type="submit" name="submit" value="Add Entry"></center></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<br><br><br>
<a href='../index.html'>Index</a>
<br>
<iframe src='checkboxsrc.php' width='100%' height='300'></iframe>
<br>
<a href='../index.html'>Index</a>
</body>
</html>