Click to See Complete Forum and Search --> : Simple flat file form submit


knowj
06-03-2010, 03:44 AM
I am currently trying to get my head round the Microsoft realms of the development coming from a PHP background.

It seems I am spolit for choice with languages meaning alot of the information I find is diluted.

At the moment I am trying to build a simple form to submit -> validate ->save to flat file (.txt) an email address then remove the form from the page and display a confirmation message.

In PHP this would take no more than 5 minutes and around 30 lines. The more I seem to read the more conflicting the information seems to be from different sources.

I would greatly appreciate it if someone could point me in the right direction.

knowj
06-03-2010, 05:36 AM
Update of what im trying to do in PHP in its simplest form (dirty and without error handeling).


Also if it would be better to use .NET for this feel free to move the thread to the .NET forum.


<?php
//check if the form has been submitted
if (isset($_POST['submit']))
{
//validate the email to a regular expression
if (preg_match('(\w+@[a-zA-Z0-9-_]+?\.[a-zA-Z]{2,6})', $_POST['email']))
{
//open the file write only, place the cursor at the bottom of the document on a new line
if (!$fp = fopen('myfile.txt', 'a'))
{
die('File cannot open or be created!');
}
//write the data to the file
if (fwrite($fp, $_POST['email']))
{
$success = 'Thankyou!';
}
//close the file
fclose($fp);
}
else
{
$error = 'Invalid Email';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form page</title>
</head>
<body>
<?php
if (isset($success)) :
echo $success;
else : ?>
<form action="" method="post">
<fieldset>
<?php
if (isset($error)) :
echo $error;
endif;
?>
<legend>Enter email</legend>
<label for="email">Email:</label>
<input type="text" name="email" id="email" value="" />
<button type="submit">Submit</button>
</fieldset>
</form>
<?php endif; ?>
</body>
</html>

yamaharuss
06-03-2010, 11:32 AM
http://www.asptutorial.info/learn/OpenReadCreate-files.html