SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    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

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And to validate an e-mail, use

    PHP Code:
    if(filter_var($user_emailFILTER_VALIDATE_EMAIL)) {
    ....
    } else {
    ....


  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    a very useful function if you are on PHP5+ orlandu63, wish they had implemented it sooner!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Woot! Thanks for the heads up.
    Saul

  6. #6
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •