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,
In the PHP script (checkbox-form. php), we can get the submitted option from the $_POST array. If $_POST['formWheelchair'] is “Yes”, then the box was checked. If the check box was not checked, $_POST['formWheelchair'] won't be set.
A checkbox allows you to select single values for submission in a form (or not).
The HTML <checkbox> tag is used to define the square boxes. It is a form element which allows users to select one or more options from the given options. It is created by the type attribute of the <input> element as shown in the following syntax: <input type="checkbox" name="field name" value="Initial value">
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With