Your best friend throughout learning the language will be the PHP manual, which will explain to you how to make use of all of the functions in PHP.
First, I'll assume you have a form set up which has a method type of "post" and a text field called "email". The easiest way to do this is to use a flat-file, instead of a database (although the database would be the better choice in the long-run, if you need to get this thing working quick a text file will do fine).
Firstly, you'll need to get your form to point to your PHP page, and have a simple PHP page set up to accept it. Once the user is sent to your page, you can get the e-mail address they entered through the $_POST superglobal.
An example of this would be:
PHP Code:
$user_email = $_POST['email'];
Next, you should make sure that the e-mail does not take more than one line (security related), using the str_replace function:
PHP Code:
$user_email = str_replace("\n", '', $user_email);
Later, once you know more about PHP, you can expand this section of code to only accept valid e-mail addresses.
Next, you'll need to become famillior with the file handling functions fopen, fwrite, and fclose.
You'll need to create a variable to store the file handle, open the e-mail file for access, write the e-mail to the file, and close the file:
PHP Code:
$file_handle = @fopen('emails.txt', 'a') or die('Could not open e-mail file.');
fwrite($file_handle, $user_email . "\n") or die('Could not write to file.');
fclose($file_handle) or die('Could not close file.');
The 'a' parameter passed to the fopen() means that the file will be opened for appending; meaning that the file will retain it's contents, and the new e-mail will be added to the last line.
The @ sign suppresses any error messages (for security reasons).
The "or die('...');" statements kill the script if any line is unsuccessful.
The "\n" in the fwrite() stands for a new line.
So, the final code (for now):
PHP Code:
//Get the user's e-mail and remove any linebreaks
$user_email = $_POST['email'];
$user_email = str_replace("\n", '', $user_email);
//Write the e-mail address to the next position in the file
$file_handle = @fopen('emails.txt', 'a') or die('Could not open e-mail file.');
fwrite($file_handle, $user_email . "\n") or die('Could not write to file.');
fclose($file_handle) or die('Could not close file.');
If you don't understand some (or all) of this right now, don't worry -- just stick with it and expand this code with the new things you learn along the way
Bookmarks