The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Help for a VERY green newbie

Hi,
I'm working on a website that was set up using PHP and mysql. I'm trying to learn, but there are a few things I need to just get done quick before I have the time to really get into PHP - Basically, I just need to add a form field to my pages that will collect email addresses to add to a list for future marketing. What is the best way to do this? Everything I've found so far on PHP forms assumes I need to process the informaton from the forms. I just want readers to be able to enter their email address and add these addresses to a file or a very simple database.
Thanks in advance for some direction. I just want to be sure I'm not missing something very basic... Like I said, I'm a very green newbie.

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.

//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

Thank you all for your help! this is huge. I think I'm going to be spending a lot of time here over the next few months as I learn this stuff! Hopefully, someday I can return the favor for a newbie...
Sandy