以 PHP 形式发送复选框值 [英] send checkbox value in PHP form

查看:31
本文介绍了以 PHP 形式发送复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

这里是表单的 php 代码,除了复选框之外的所有内容.当我收到邮件时,我需要知道它的价值.例如:姓名:John,电子邮件:test@test.com,电话:12345,时事通讯:是"

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 .= "
 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 .= "
 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:
 Name: $name 
 Email: $email_address 
 Tel 
 $message
 Newsletter 
 $newsletter"
}
    ; 


    $headers = "From: $myemail
"; 
    $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>

谢谢,

推荐答案

这是在检查时返回一个简单的 Yes 的样子.

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.

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

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.

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

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

在您现有的代码之下:

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

添加:

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

如果选中该复选框,它将在您的电子邮件中添加Yes,如果未选中,它将添加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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆