<?php
$dir 
'basic';
$doc 'email.txt';
$file "$dir/$doc";

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!!!';
    }
}
?>

<html>
<head>
<title>Week #10 BASIC ~ 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 />
<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 $value){
        
$row_data explode("¥" $value);
        if(
$value != ''){
            print(
"<tr><td>$row_data[0]</td><td>$row_data[1]</td></tr>");
        }
    }
    
} else {
    print(
"There's a problem reading the file");
}
?>
</table><br clear="left"/>











<br />
<a href='../index.html'>Index</a>
<iframe src='wk10basicsrc.php' width='100%' height='300'></iframe>
<a href='../index.html'>Index</a>

</body>
</html>