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: [email protected], Tel:12345, Newsletter: Yes"
<?php
$errors = '';
$myemail = '[email protected]';//<-----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,
if(isset($_POST["newsletter"]) && $_POST["newsletter"] == "newsletter"){
//checked
}
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
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.
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";
Source: Stackoverflow.com