SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php Form to email with Checkbox

    Hey guys,

    I have a contact form with a image verification and a checkbox to sign up for a newsletter or not. What I'm trying to do is send an extra email to a email address if the checkbox to sign up for a newsletter is checked. The newsletter system I have signs up people by sending a email to a email address with the subject line of "subscribe". I used an array because I need to send it to two email address, but it's not a requirement. I think I got the code close, but I can't see to get it to work. Any help would be appreciated! Thanks!

    Here is the HTML:

    HTML Code:
    <form action="mailer.php" method="post" name="form" id="form" onsubmit="MM_validateForm('name','','R','from','','RisEmail','verif_box','','R','message','','R','newsletter','','');return document.MM_returnValue">
                        <tr>
                          <td width="42">Name</td>
                          <td width="200" colspan="2"><input name="name" type="text" id="name" size="20" value="<?php echo $_GET['name'];?>" /></td>
                        </tr>
                        <tr>
                          <td>Email</td>
                          <td colspan="2"><input name="from" type="text" id="from" size="20" value="<?php echo $_GET['from'];?>" /></td>
                        </tr>
                        <tr>
                          <td colspan="3"><textarea name="message" id="message" cols="30" rows="4"><?php echo $_GET['message'];?></textarea></td>
                        </tr>
                        <tr>
                          <td>Verify: </td>
                          <td colspan="2"><input name="verif_box" type="text" id="verif_box" size="10" />
                            <img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /></td>
                        </tr>
                        <tr>
                          <td colspan="3"><input type="checkbox" name="newsletter[]" value="yes" />Sign me up for the Newsletter!
                            &nbsp;&nbsp;&nbsp;
                            <input name="Submit" type="submit" value="Submit" /></td>
                        </tr>
                      </form>
    Here is the PHP (mailer.php):

    PHP Code:
    <?php
    // ----------------------------------------- 
    //  Contact & Newsletter Mail Form
    // ----------------------------------------- 

    // load the variables form address bar
    $name $_REQUEST["name"];
    $subject "Contact Form";
    $newssubject "Subscribe";
    $message $_REQUEST["message"];
    $from $_REQUEST["from"];
    $verif_box $_REQUEST["verif_box"];
    $newsletter $_REQUEST["newsletter"];

    // remove the backslashes that normally appears when entering " or '
    $message stripslashes($message); 
    $from stripslashes($from); 
     
    // check to see if verificaton code was correct
    if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){

        
    // check if newsletter checkbox is checked and create email
        
    if(isset($_REQUEST['newsletter'])) 
    {

    // create array for newsletter recipients
        
    $newsyes = array( "yes" => array('newsletter1@email.com','newsletter2@email.com));
        foreach($_REQUEST['
    newsletter'] as $value){foreach($newsyes[$value] as $value2){mail($value2,$newssubject);}}

    }

        // if verification code was correct send the message and show this page
        mail("contact@email.com",$subject, $message, "From: $from");
        mail($newsyes, $newssubject, $message, "From: $from");
        // delete the cookie so it cannot sent again by refreshing this page
        setcookie('
    tntcon','');
    } else {
        // if verification code was incorrect then return to contact page and show error
        header("Location:".$_SERVER['
    HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
        exit;
    }
    ?>

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See how in the //create array for newsletter recipients section everything turns red in your code? That tells you that there's a syntax error with the code. You are missing the closing ' after 'newsletter2@email.com

    You've really over complicated that section. There's no need to make newsletter an array, which can only ever contain a single value of 'yes' (otherwise not exist), and then loop (just to get the 'yes') value, and lookup another array.
    Try something like this:

    PHP Code:
    //email newsletter recipients if checked
    if(isset($_REQUEST['newsletter'])) {
        
    mail('newsletter1@email.com'$newssubject);
        
    mail('newsletter2@email.com'$newssubject);

    btw, doing the strip slashes is not very portable. A different (better) host might have magic quotes turned off, so those extra slashes won't be there. I'd recommend using a .htaccess or php.ini file to turn magic quotes off instead.

  3. #3
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just tried that... I get the first email from the contact form fine still, but the other 2 do not send. Thanks for the help so far! Any idea why it wouldn't be sending still?

  4. #4
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Man, I cannot get this stupid form to work properly. Anyone else got any suggestions?

    * BUMP *

  5. #5
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try putting the e-mails in an array, and then running a foreach over the array:

    PHP Code:
    //email newsletter recipients if checked
    if(isset($_REQUEST['newsletter'])) {
    $emails = array(
       
    => 'newsletter1@email.com'
       
    => 'newsletter2@email.com'
    );

    foreach (
    $emails as $email){
        
    mail($email$newssubject);
    //end foreach

    //close if statement 
    ~ Nate L ~

  6. #6
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't the value of the checkbox have to be "1" or "2"? This could work if I had 2 checkboxes, but I only have one. Upon checked, it should send an email to two different email addresses.

  7. #7
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kevinptv View Post
    Wouldn't the value of the checkbox have to be "1" or "2"? This could work if I had 2 checkboxes, but I only have one. Upon checked, it should send an email to two different email addresses.
    Yeah,

    Change your input checkbox name to just "newsletter", not "newsletter[]"

    On the receiving side of things,
    PHP Code:
    if ($_POST['newsletter'] == 1){
    // processing code here

    the checkbox will either be a 1 (true) or 0 (false)

    edit: I also had my checkbox value set to "1", whereas you have yours set to "yes". I think it's easier to evaluate against a 1 or zero versus a "yes" or "no" string. Just my preference.
    ~ Nate L ~

  8. #8
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's my little dummy script I threw together.

    PHP Code:
    <?php 
    if($_POST){
        if ((int)
    $_POST['newsletter'] == 1){
            echo 
    "Selected";
        }
    }
    ?>

    <br /><br />

    <?php
    $emails 
    = array(
       
    => 'newsletter1@email.com'
       
    => 'newsletter2@email.com'
    );

    foreach (
    $emails as $email){
        echo 
    $email "<br />";


    ?>

    <form method="post" action="">
    <input type="checkbox" name="newsletter" value="1" />
    <input type="submit" value="submit" />
    </form>
    ~ Nate L ~

  9. #9
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so I set the checkbox to:

    HTML Code:
    <input type="checkbox" name="newsletter" value="1" />
    And I set the mail function to:

    PHP Code:
    //email newsletter recipients if checked 
    if ($_POST['newsletter'] == 1){ 
        
    mail('newsletter1@email.com'$newssubject);
        
    mail('newsletter2@email'$newssubject);

    Stil doesn't work. The main email sends fine, but not the other 2.

  10. #10
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AWESOME! That worked. Thanks so much for your help.

  11. #11
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kevinptv View Post
    AWESOME! That worked. Thanks so much for your help.
    What worked? Putting the mail() function in a Foreach loop?
    ~ Nate L ~

  12. #12
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeup! THanks!

  13. #13
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good dealio.

    I refined that little test script some more, just for my own sake (i'm still learning this stuff too)...here's the updated code that only displays the e-mails if you do have the box checked

    PHP Code:
    <?php 
    if($_POST){
        if ((int)
    $_POST['newsletter'] == 1){
            
    $emails = array(
                   
    => 'newsletter1@email.com'
                   
    => 'newsletter2@email.com'
                
    );

            foreach (
    $emails as $email){
                echo 
    $email "<br />";
            } 
        }
    }
    ?>

    <form method="post" action="">
    <input type="checkbox" name="newsletter" value="1" />
    <input type="submit" value="submit" />
    </form>
    ~ Nate L ~

  14. #14
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if((int)$_POST['newsletter']) will raise a notice error if you have your error reporting set to strict. This is because $_POST['newsletter'] may or may not exist (depending on whether it was checked). If you attempt to do something with a variable that doesn't exist PHP raises a notice.

    You only really need to determine if $_POST['newsletter'] EXISTS (isset) to know if the checkbox was checked. The value is irrelevant really.

  15. #15
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    if((int)$_POST['newsletter']) will raise a notice error if you have your error reporting set to strict. This is because $_POST['newsletter'] may or may not exist (depending on whether it was checked). If you attempt to do something with a variable that doesn't exist PHP raises a notice.

    You only really need to determine if $_POST['newsletter'] EXISTS (isset) to know if the checkbox was checked. The value is irrelevant really.
    Good info! Thanks
    ~ Nate L ~

  16. #16
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is really useful also for me, I am in the same situation now and I have been searched for a solution.

  17. #17
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi kevinptv
    Thank you so much for useful post. I shall aply it on my site soon.
    thanx again


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
  •