以PHP形式发送复选框值 [英] send checkbox value in PHP form
问题描述
我有一个关于php表单的问题。我已经添加一个复选框到现有的表单,但不知道如何将其添加到PHP。
< form method =POSTname =contactformaction =contact-form-handler.php>
< p>
< input type =textname =nameplaceholder =name>
< / p>
< p>
< input type =telname =telplaceholder =phome> < br>
< / p>
< p>
< input type =textname =emailplaceholder =mail> < br>
< / p>
< p>
< input type =checkboxname =newsletter []value =newsletterchecked>我想注册电子报< br>
< / p>
< input type =submitvalue =Submit>< br>
< / form>
这里是表单的PHP代码,除了复选框以外的所有东西。我需要知道它的价值,当我收到邮件。例如:Name:John,Email:test@test.com,Tel:12345,Newsletter:Yes
?php
$ errors ='';
$ myemail ='test@gmail.com'; //< -----把你的电子邮件地址放在这里。
if(empty($ _ POST ['name'])||
empty($ _ POST ['email'])||
empty($ _ POST ['tel']))
{
$ errors。=\\\
错误:所有字段都是必填的;
}
$ 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 - ] +)*(\。[az] {2,3})$ / i,
$ email_address))
{
$ errors。=\\\
错误:电子邮件地址无效;
}
if(empty($ errors))
{
$ to = $ myemail;
$ email_subject =联系表单提交:$ name;
$ email_body =您收到了一条新消息。。
详细信息:\\\
名称:$ name \\\
电子邮件:$ email_address \\\
电话\\\
$ message\\\
通讯\\\
$ newsletter
}
;
$ headers =From:$ myemail\\\
;
$ headers。=回复:$ email_address;
mail($ to,$ email_subject,$ email_body,$ headers);
//重定向到谢谢页面
header('Location:contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< title>联系表单处理程序< / title>
< / head>
< body>
<! - 此页面仅在出现错误时显示 - >
<?php
echo nl2br($ errors);
?>
< / body> ;,
< / html>
谢谢,
pre>
< input type =checkboxid =newslettername =newslettervalue =Yeschecked>
< label for =newsletter>我想注册电子报< / label>
我还添加了文本作为标签,这意味着您可以单击文本以检查框。
如果复选框被选中,表单提交时 $ _ POST ['newsletter']
将等于是
。你如何检查 $ _ POST ['name']
, $ _ POST ['email']
和 $ _ POST ['tel']
都是空的,你可以这样做。
您可以将其添加到您的php端的电子邮件中:
在您现有的代码下:
$ name = $ _POST ['name'];
$ email_address = $ _POST ['email'];
$ message = $ _POST ['tel'];
添加:
code> $ newsletter = $ _POST ['newsletter'];
if($ newsletter!='Yes'){
$ newsletter ='No';
}
如果选中此复选框,则会添加 Yes
在您的电子邮件中,如果未选中它将添加否
。
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,
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
.
这篇关于以PHP形式发送复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!