Php form with checkbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kevinptv
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 3:58 pm

Php form with checkbox

Post by kevinptv »

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:

Code: Select all

 
<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):

Code: Select all

 
<?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;
}
?>
 
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Php form with checkbox

Post by Robert07 »

You say you are close, what happens when you try to use the code now? Are there any errors?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Php form with checkbox

Post by Ollie Saunders »

Robert07 is correct to ask the question he does. These points may help:
  • You're using some JavaScript to validate this form. Is that causing any problems? To be certain you could temporarily remove those validations whilst testing your PHP. Note that validations should be carried out on the server-side in addition to any client-side ones because you can't rely on JavaScript being on or available.
  • Prefer $_GET or $_POST to use of the $_REQUEST variable for security reasons.
  • The GET param being passed to your image verification

    Code: Select all

    <img src="verificationimage.php?<?php echo rand(0,9999);?>"
    could be easily read without needing to interpret the image. It'll provide some security simply by being an extra hoop to jump through but won't prevent specifically crafted automated submissions.
  • I don't understand why you're using an array for the checkbox.

    Code: Select all

    <input type="checkbox" name="newsletter[]" value="yes" />
    You only have one right? The array will provide no extra information. If you want to put a value into an array in your PHP code you may either place it within one:

    Code: Select all

    $oneElemArray = array($_GET['someValue']);
    or cast it to an array:

    Code: Select all

    $oneElemArray = (array)$_GET['someValue'];
    the latter is my preference.
  • Your use of strip_slashes irks me because it indicates that your code may be relying on magic_quotes. I outline the best practise for this in this brief post.
  • Code: Select all

    $newsyes = array( "yes" => array('newsletter1@email.com','newsletter2@email.com));
     
    could be more simply expressed as:

    Code: Select all

    $newsRecipients = array('newsletter1@email.com', 'newsletter2@email.com');
    Also you were missing a closing quote there.
  • Here:

    Code: Select all

    header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
    you should pre-process the variables being used as GET parameters in URLs with urlencode().
These problems and potential problems emerged from a cursory glance at your code; there could be more. Hope this helps.
kevinptv
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 3:58 pm

Re: Php form with checkbox

Post by kevinptv »

Wow, thanks for the input.

I have tried removing the verification code and still no luck. Bottom line is, the main contact email comes through fine. The secondary emails however, do not. I've even simplified it enough down to:

Code: Select all

if(isset($_REQUEST['newsletter'])) {
    mail('newsletter1@email.com', $newssubject);
    mail('newsletter2@email.com', $newssubject);
} 
 
And still no luck (And I did realize I left the close quote out of the original array. That was done by mistake when I replaced my real email addresses with dummy addresses).

So now that I removed the array, the check box now reads as:

Code: Select all

<input type="checkbox" name="newsletter" value="yes" />
I've never been any good at forms+PHP+validation+checkboxes. Lol. Any more help would be appreciated. Thanks!
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Php form with checkbox

Post by Robert07 »

What does the MM_validateForm function do? Does it just set the value of some document.MM_returnValue object or does it return true/false?

Code: Select all

 
MM_validateForm('name','','R','from','','RisEmail','verif_box','','R','message','','R','newsletter','','');
return document.MM_returnValue
 
What you're saying now is that

Code: Select all

 
mail("contact@email.com",$subject, $message, "From: $from");
 
sends an email fine, but

Code: Select all

 
mail('newsletter1@email.com', $newssubject);
 
does not?

Or does the first email get sent but not the second?

Code: Select all

 
mail('newsletter1@email.com', $newssubject);
 
but not

Code: Select all

 
mail('newsletter2@email.com', $newssubject);
 
kevinptv
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 3:58 pm

Re: Php form with checkbox

Post by kevinptv »

The javascript validation simply returns you to the page and highlights the fields that are required and/or says the verification image is wrong.

The main email, which is the contact@email.com, is sent fine. The if(isset) function however, is not. It doesn't send out either email at all. If I get rid of the if() statement and simply make the checkbox return a yes or no by using $_REQUEST['newsletter'] it seems to pull the result fine. Obviously I guess I could make the value of the checkbox be an email address, but then the address would undoubtably be harvested out of the HTML and spammed to kingdom come.
kevinptv
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 3:58 pm

Re: Php form with checkbox

Post by kevinptv »

BTW, you have allot of kids if that is you in the photos on your websites. How do you manage to be a web developer? Lol.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Php form with checkbox

Post by Robert07 »

If the isset function is not working, then try printing the post array at the top of your page like this:

Code: Select all

 
echo "POST:<br>";
print_r($_POST);
 
Then submit the form with and without the box checked and see what is being passed. Normally whatever you put into a checkbox as a value will not be passed (you'll get "on" as the value instead), but you should see something different when you check or uncheck the box, unless the checkbox is outside the form now...

Yes those are my kids, some go to school and others go to daycare so my wife and I can both get some work done during the week. :)
kevinptv
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 3:58 pm

Re: Php form with checkbox

Post by kevinptv »

I figured it out with some help from another programmer, so if anyone is interested how to do this, this is the code I used. Thanks for all your help!!

Code: Select all

if($_POST){ 
    if ((int)$_POST['newsletter'] == 1){ 
        $emails = array( 
   1 => 'newsletter1@email.com', 
   2 => 'newsletter2@email.com'); 
 
    } 
} 
 
foreach ($emails as $email){ 
    mail($email,$newssubject, $message, "From: $from");
}
Post Reply