Hello,

I'm currently using a basic formmail.pl script for the forms on my website. I have a newsletter signup option on all of my forms for visitors to subscribe. Up until now, I have been importing the email addresses and names into a text file for my mailing list. I basically copy and paste each entry. Since the newsletter is reaching 2,000 entries and the signup have been increasing every day, this is becoming a major burden on managing.

I would like to get some assistance for creating a PHP form which will do all of these tasks:

  1. Only capture email address and first name for visitors who check the newsletter signup box.
  2. Output the visitors email address and name into a flat file on the server.
  3. Format the email and name like this:

"anyone1@email.com","Name"
"anyone2@email.com","Name"
"anyone3@email.com","Name"

  1. Check and make sure any duplicate email addresses will not be added to the text file.

You can review one of my current forms here:

Any help would be greatly appreciated.

Thanks!

    The way it's being done here is as follows: you write the code - we help you with errors (or point you in the right direction). Not opposite - you give directions and somebody writes the code.

    1. For this task $_POST array is neccessary.
    2. [man]fopen[/man], [man]fwrite[/man], [man]fclose[/man] may come in handy here.
    3. I would ommit double quotes if I were you. Other then that, it's just writing some varibles and strings using dot operator
      fwrite($f, $email.",".$name);
    4. Prior to writing to file, use [man]file[/man] to read a file, each line in each element of an array. Use [man]foreach[/man] to go through an array line by line, and [man]split[/man] each line to get email address. Then compare it with new email to see if it's not there already.

    Today's posts are sponsored by:
    The great...
    The mine of information...
    The lighthouse on the sea of lack of knowledge...
    The "knowing nearly all about php"...
    The fountain of wisdom...

    ...Ladieeeeees and Gentelmaaaaan...

    The PHP Manual🙂

      I don't see a checkbox for newsletter signup, but here's an outline of how the rest of it could be done with the form you have:

      $name_arr = explode($_POST['FullName'])
      $first_name = $name_arr[0];
      $file_arr = file($flat_file);
      $found = false;
      foreach ($file_arr as $value) {
          if (strpos($_POST['email']) !== false) {
              $found = true;
              break;
          }
      }
      if (!$found) {
          $fp = fopen($flat_file, 'a');
          $fwrite($fp,$_POST['email'] . ',' . $_POST['FullName'] . "\n");
          fclose($fp);
      )

      You would need to add error checking, validity checking, completeness checking, etc.

        Installer-

        Thanks for the details.

        An example where the newsletter signup is part of the form can be reveiwed here:

        Can I add the code you included to my current form page so it works off the formmail.pl script or do I need to create a new php page for this? I'm very new to php 🙂

        Thanks!

          You can use it as long as you learn from it, to figure out why or why not it works, and what's right and what's wrong with it.

          But I know that's not what you're asking. Whether you can use it from your pearl script or not depends. What is the script and what does it do? Does it post its form values? Do they have the same names as the homepage form?

            I'll have to look at it tomorrow, bad. Maybe meanwhile someone else will help you out.

              Originally posted by bad959fl
              http://www.shapefit.com/quiz.html

              Can I add the code you included to my current form page so it works off the formmail.pl script or do I need to create a new php page for this? I'm very new to php 🙂

              That form is action="quiz.pl" for testing form input.

              But of course you can use PHP instead of Perl (.pl).
              An make action="formprocessing.php", and write a 'formprocessing.php' using PHP.

              This is the present form, in quiz.html

              <form method="POST" action="http://www.shapefit.com/cgi-bin/quiz.pl">
                            <input type=hidden name="recipient" value="quiz@shapefit.com">
                            <input type=hidden name="subject" value="Fitness I.Q. Quiz Entry">
                            <input type=hidden name="print_config" value="email,subject">
                            <input type=hidden name="redirect"value="http://www.shapefit.com/thankyou-quiz.html">
                            <input type=hidden name="env_report" value="REMOTE_HOST, HTTP_USER_AGENT">
                            <input type=hidden name="required" value="FullName,email,Referred By">
                            <div align="center"> 
                              <p><br>
                                *Full Name&nbsp;&nbsp; <br>
                                <input type="text" name="FullName" size="25">
                              </p>
                              <p>*Email Address:<br>
                                <input type="text" name="email" size="25">
                              </p>
                              <p>*How did you find us?<br>
                                <input type="text" name="Referred By" size="25">
                              </p>
                              <p>*If you found us through a search <br>
                                engine, what were you searching for?<br>
                                <input type="text" name="Searching For" size="25">
                              </p>
                              <p> 
                                <input type="checkbox" name="Yes, Sign Me Up For The Newsletter" value="checkbox" checked>
                                Yes, I would like to sign up for your <b>FREE</b> newsletter!<br>
                                <br>
                                <br>
                                <input name="subscribe" type="image" alt="fitness quizzes" src="start-quiz2.gif">
                              </p>
                            </div>
                          </form>

              *** fotenote
              somefile.pl -> .pl stands for Perl which is another programming language.

                halojoy-

                The quiz.pl was simply renamed from the original "formmail.pl" script. They are identical other than the file name.

                I'm looking for the examples or assistance on how to write the actual php form page itself, to basically work like the "fommail.pl" script is now but also doing all of the tasks needed in terms of the output to a text file. I'm not sure if this would be too tough for someone to help me out with but it would be extremely helpful.

                Thanks!

                  I'd rather give assistance then full working example. It's not because I don't want to help you, but I want you to gain some knowledge.
                  If you haven't done this already, read some simple tutorial of php (many can be found on the net).
                  Then, in halojoy's form change action to some php file (like process.php) and in that file put Installer's script (in php tags - the first thing you'll learn from php tutorial).
                  If you want to add checkbox checking, change the name of the checkbox input to something simpler (e.g. "newsletter") and then use if clause:

                  if (isset($_POST['newsletter'])&&$_POST['newsletter']=="checkbox")
                  {
                     //Installer's code here
                  }
                  

                  Try to do it yourself and in case of any trouble, ask here.

                    wilku-

                    I created a new form with this code:


                    <form method="POST" action="process.php">

                    <input type=hidden name="recipient" value="quiz@shapefit.com">
                    <input type=hidden name="subject" value="Fitness I.Q. Quiz Entry">
                    <input type=hidden name="print_config" value="email,subject">
                    <input type=hidden name="redirect"value="http://www.shapefit.com/thankyou-quiz.html">
                    <input type=hidden name="env_report" value="REMOTE_HOST, HTTP_USER_AGENT">
                    <input type=hidden name="required" value="FullName,email,Referred By">
                    <div align="center">

                    <p><br>
                    Full Name&nbsp;&nbsp; <br>
                    <input type="text" name="FullName" size="25">
                    </p>
                    <p>
                    Email Address:<br>
                    <input type="text" name="email" size="25">
                    </p>
                    <p>How did you find us?<br>
                    <input type="text" name="Referred By" size="25">
                    </p>
                    <p>
                    If you found us through a search <br>
                    engine, what were you searching for?<br>
                    <input type="text" name="Searching For" size="25">
                    </p>
                    <p>

                    <input type="checkbox" name="Yes, Sign Me Up For The Newsletter" value="checkbox" checked>
                    Yes, I would like to sign up for your <b>FREE</b> newsletter!<br>
                    <br>
                    <br>
                    <input name="subscribe" type="image" alt="fitness quizzes" src="start-quiz2.gif">
                    </p>
                    </div>

                    </form>

                    I then created a file called "process.php" and included this code:


                    <?php
                    if (isset($POST['newsletter'])&&$POST['newsletter']=="checkbox")
                    {
                    $name_arr = explode($POST['FullName'])
                    $first_name = $name_arr[0];
                    $file_arr = file($flat_file);
                    $found = false;
                    foreach ($file_arr as $value) {
                    if (strpos($
                    POST['email']) !== false) {
                    $found = true;
                    break;
                    }
                    }
                    if (!$found) {
                    $fp = fopen($flat_file, 'a');
                    $fwrite($fp,$POST['email'] . ',' . $POST['FullName'] . "\n");
                    fclose($fp);
                    )
                    }

                    ?>

                    However, now I'm receiving this error when submitting the form:

                    Parse error: parse error in /home/httpd/vhosts/aimtraffic.com/httpdocs/test/process.php on line 5

                    Line 5 is:

                    $first_name = $name_arr[0];

                      // you forget to end this line with a semicolon   ;
                      $name_arr = explode($_POST['FullName']);

                      Otherwise, you have done a very good job!
                      Congratulations and welcome into our php programming gang
                      😃

                        some code at last 😉

                        A word about $POST: as you probably noticed, $POST has all the values, which have had an input field in a form. So for example:

                        <!-- in a form -->
                         <input type="text" name="FullName" size="25">
                        
                        <?php
                        //in form processing
                        $name_arr = explode($_POST['FullName']) ;
                        ?>
                        

                        Now look closely at your script. You're using $_POST['newsletter'], but there's no such input in a form with a name="newsletter". So the condition isset($_POST['newsletter']) will never be fulfilled and the data would never be saved. In order for the script to work you need to rename checkbox input in a form to "newsletter".

                          <input type="checkbox" name="Yes, Sign Me Up For The Newsletter" value="checkbox" checked> 
                          Yes, I would like to sign up for your <b>FREE</b> newsletter!<br>

                          It is here we need some changes
                          maybe: name="newsletter" ???

                            halojoy - you're spoiling all the fun for bad959fl🙂 😉
                            There's no place now for a detective work on how to make this script work 😃
                            There's no satisfaction if you have everything given on a plate.

                              🙁 sorry for this - you are right, wilku

                                Hey Guys-

                                Thanks for all your help so far!

                                I have made a few updates as you noted to the code but I still get this error message when submitting the form:


                                Parse error: parse error in /process.php on line 18

                                My full code for both files is:

                                form.html

                                <form method="POST" action="process.php">
                                <input type=hidden name="recipient" value="quiz@shapefit.com">
                                <input type=hidden name="subject" value="Fitness I.Q. Quiz Entry">
                                <input type=hidden name="print_config" value="email,subject">
                                <input type=hidden name="redirect"value="http://www.shapefit.com/thankyou-quiz.html">
                                <input type=hidden name="env_report" value="REMOTE_HOST, HTTP_USER_AGENT">
                                <input type=hidden name="required" value="FullName,email,Referred By">
                                <div align="center">

                                <p><br>
                                Full Name&nbsp;&nbsp; <br>
                                <input type="text" name="FullName" size="25">
                                </p>
                                <p>
                                Email Address:<br>
                                <input type="text" name="email" size="25">
                                </p>
                                <p>How did you find us?<br>
                                <input type="text" name="Referred By" size="25">
                                </p>
                                <p>
                                If you found us through a search <br>
                                engine, what were you searching for?<br>
                                <input type="text" name="Searching For" size="25">
                                </p>
                                <p>

                                <input type="checkbox" name="newsletter" value="newsletter" checked>
                                Yes, I would like to sign up for your <b>FREE</b> newsletter!<br>
                                <br>
                                <br>
                                <input name="subscribe" type="image" alt="fitness quizzes" src="start-quiz2.gif">
                                </p>
                                </div>

                                </form>

                                process.php

                                <?php
                                if (isset($POST['newsletter'])&&$POST['newsletter']=="checkbox")
                                {
                                $name_arr = explode($POST['FullName']);
                                $first_name = $name_arr[0];
                                $file_arr = file($flat_file);
                                $found = false;
                                foreach ($file_arr as $value) {
                                if (strpos($
                                POST['email']) !== false) {
                                $found = true;
                                break;
                                }
                                }
                                if (!$found) {
                                $fp = fopen($flat_file, 'a');
                                $fwrite($fp,$POST['email'] . ',' . $POST['FullName'] . "\n");
                                fclose($fp);
                                )
                                }

                                ?>

                                Thanks!

                                  Look at the error message:

                                  Parse error: parse error, unexpected ')' in process.php on line 18

                                  As you can see the ")" is not expected there - blocks of code are opened and closed with "{" and "}". So put "}" instead of ")" in line 18.
                                  Other then that $flat_file is not defined in your php script. It should contain local path to file where you want to store your data e.g.

                                  $flat_file = '/home/user/form_data.txt';//linux
                                  $flat_file = 'c:/www/user/form_data.txt';//windows
                                  

                                  And the last thing: you don't use those hidden inputs at the beginning of a form, so you can ommit them as well.

                                    wilku-

                                    Now I get an error about line 9. Line 9 in the file is:


                                    if (strpos($_POST['email']) !== false) {

                                    I updated the 'process.php' file to:


                                    <?php
                                    if (isset($POST['newsletter'])&&$POST['newsletter']=="checkbox")
                                    {
                                    $name_arr = explode($POST['FullName']);
                                    $first_name = $name_arr[0];
                                    $file_arr = file($flat_file = 'form_data.txt'');
                                    $found = false;
                                    foreach ($file_arr as $value) {
                                    if (strpos($
                                    POST['email']) !== false) {
                                    $found = true;
                                    break;
                                    }
                                    }
                                    if (!$found) {
                                    $fp = fopen($flat_file, 'a');
                                    $fwrite($fp,$POST['email'] . ',' . $POST['FullName'] . "\n");
                                    fclose($fp);
                                    }
                                    }

                                    ?>

                                    Thanks!