7

I have a question regarding a php form. I've added a checkbox to the existing form, but not sure how to add it to the php. I would like it to send "yes" if the visitores checks it, and "no" if he is not.

<form method="POST" name="contactform" action="contact-form-handler.php"> 
<p>
<input type="text" name="name" placeholder="name">
</p>
<p>
<input type="tel" name="tel" placeholder="phome"> <br>
</p>
<p>
<input type="text" name="email" placeholder="mail"> <br>
</p>
<p>
<input type="checkbox" name="newsletter[]" value="newsletter" checked>i want to sign up   for newsletter<br>
</p>
<input type="submit" value="Submit"><br>
</form>

here is the php code for the form, everything there except the checkbox. i need to know its value when i receive the mail. for example : "Name: John, Email: test@test.com, Tel:12345, Newsletter: Yes"

<?php 
$errors = '';
$myemail = 'test@gmail.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['tel']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['tel']; 


if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Tel \n $message\n Newsletter \n $newsletter"
}
    ; 


    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

Thank you,

Pavel
  • 153
  • 1
  • 2
  • 12
  • 3
    `print_r($_POST);` you will see what values you have to work with –  Nov 10 '13 at 19:15

5 Answers5

15

Here's how it should look like in order to return a simple Yes when it's checked.

<input type="checkbox" id="newsletter" name="newsletter" value="Yes" checked>
<label for="newsletter">i want to sign up for newsletter</label>

I also added the text as a label, it means you can click the text as well to check the box. Small but, personally I hate when sites make me aim my mouse at this tiny little check box.

When the form is submitted if the check box is checked $_POST['newsletter'] will equal Yes. Just how you are checking to see if $_POST['name'],$_POST['email'], and $_POST['tel'] are empty you could do the same.

Here is an example of how you would add this into your email on the php side:

Underneath your existing code:

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['tel'];

Add:

$newsletter = $_POST['newsletter'];
if ($newsletter != 'Yes') {
    $newsletter = 'No';
}

If the check box is checked it will add Yes in your email if it was not checked it will add No.

Kirill Fuchs
  • 13,446
  • 4
  • 42
  • 72
  • 1
    i am not sure where exactly to put the php snippet you sent, can you please direct me? – Pavel Nov 10 '13 at 20:19
  • 2
    @Pavel I edited my answer, to show you where to place the code. – Kirill Fuchs Nov 10 '13 at 20:32
  • 1
    Hey Kiril, thanks for your answer, i amended the code, but now it sends "NO" no matter if the check-box is checked or not: You have received a new message. Here are the details: Name: dfg Email: test@tes.com Tel erter Newsletter No – Pavel Nov 10 '13 at 20:49
  • 2
    @Pavel did you change the html for your check box to look like this > ``? – Kirill Fuchs Nov 10 '13 at 20:51
  • yed i did, but the mail keeps coming with "NO" – Pavel Nov 10 '13 at 20:55
  • 2
    @Pavel You can try debugging by seeing what the `$newsletter` variable equals after the if statement. After the if statement you can add `die($newsletter);` and what ever prints on your screen is what will be added to the email. It's working for me, so I'm thinking you might have a typo somewhere or some misplaced code. – Kirill Fuchs Nov 10 '13 at 21:03
12

If the checkbox is checked you will get a value for it in your $_POST array. If it isn't the element will be omitted from the array altogether.

The easiest way to test it is like this:

if (isset($_POST['myCheckbox'])) {
  $checkBoxValue = "yes";
} else {
  $checkBoxValue = "no";
}

For your code, add it immediately below the other preprocessing:

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['tel']; 

if (isset($_POST['newsletter'])) {
  $newsletter = "yes";
} else {
  $newsletter = "no";
}

You'll also need to change the HTML slightly. Change this line:

<input type="checkbox" name="newsletter[]" value="newsletter" checked>i want to sign up for newsletter<br>

to this:

<input type="checkbox" name="newsletter" value="newsletter" checked>i want to sign up   for newsletter<br>
                                      ^^^ remove square brackets here.
2
if(isset($_POST["newsletter"]) && $_POST["newsletter"] == "newsletter"){
    //checked
}
1

try changing this part,

<input type="checkbox" name="newsletter[]" value="newsletter" checked>i want to sign up   for newsletter

for this

<input type="checkbox" name="newsletter" value="newsletter" checked>i want to sign up   for newsletter
John
  • 544
  • 4
  • 19
  • To elaborate, `newsletter[]` might seem like an array value, but `newsletter[]` would parse as a plain variable named `newsletter[]` if that works at all. – Giacomo1968 Nov 10 '13 at 19:21
  • @JakeGould `name="newsletter[]"` would return an array. `$_POST['newsletter']` would = `Array([0]=>newsletter)`. – Kirill Fuchs Nov 10 '13 at 19:43
  • yes you correct regarding the array, but still how can i print it in tha mail i receive? as mentioned i would like to receive the value checked in this checkbox to my mail when user submits. – Pavel Nov 10 '13 at 20:17
0

replace:

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['tel']; 

with:

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['tel'];
if (isset($_POST['newsletter'])) {
  $checkBoxValue = "yes";
} else {
  $checkBoxValue = "no";
}

then replace this line of code:

$email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Tel \n $message\n Newsletter \n $newsletter"

with:

$email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Tel \n $message\n Newsletter \n $newsletter";
abhiklpm
  • 1,603
  • 2
  • 16
  • 21
  • can you please direct me where exactly i should put this code so it will print it in the mail? – Pavel Nov 10 '13 at 20:16