以 PHP 形式发送复选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19893927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:11:20  来源:igfitidea点击:

send checkbox value in PHP form

phpformscheckbox

提问by Pavel

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.

我有一个关于 php 表单的问题。我在现有表单中添加了一个复选框,但不确定如何将其添加到 php.ini 文件中。如果访问者检查它,我希望它发送“是”,如果他没有检查,则发送“否”。

<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代码,除了复选框之外的所有内容。当我收到邮件时,我需要知道它的价值。例如:“姓名:约翰,电子邮件:[email protected],电话:12345,时事通讯:是”

<?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,

谢谢,

回答by Kirill Fuchs

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

为了在Yes检查时返回一个简单的,它应该是什么样子的。

<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.

提交表单时,如果选中该复选框$_POST['newsletter']将等于Yes. 只要你是如何检查是否$_POST['name']$_POST['email']以及$_POST['tel']是空的,你可以这样做。

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

以下是如何在 php 端将其添加到电子邮件中的示例:

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 Yesin your email if it was not checked it will add No.

如果选中该复选框,它将添加Yes到您的电子邮件中,如果未选中,它将添加No.

回答by Kirill Fuchs

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

如果选中该复选框,您将在$_POST数组中获得它的值。如果不是,则元素将从数组中完全省略。

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:

您还需要稍微更改 HTML。更改此行:

<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.

回答by Heinrich

if(isset($_POST["newsletter"]) && $_POST["newsletter"] == "newsletter"){
    //checked
}

回答by John

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

回答by abhiklpm

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";