6

我有一个关于 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>

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

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

谢谢,

4

5 回答 5

12

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

<input type="checkbox" id="newsletter" name="newsletter" value="Yes" checked>
<label for="newsletter">i want to sign up for newsletter</label>

我还将文本添加为​​标签,这意味着您也可以单击文本以选中该框。虽小,但我个人讨厌网站让我将鼠标对准这个小小的复选框。

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

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

在您现有的代码下方:

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

添加:

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

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

于 2013-11-10T19:37:14.990 回答
11

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

最简单的测试方法是这样的:

if (isset($_POST['myCheckbox'])) {
  $checkBoxValue = "yes";
} else {
  $checkBoxValue = "no";
}

对于您的代码,将其添加到其他预处理下方:

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

if (isset($_POST['newsletter'])) {
  $newsletter = "yes";
} else {
  $newsletter = "no";
}

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

<input type="checkbox" name="newsletter[]" value="newsletter" checked>i want to sign up for newsletter<br>

对此:

<input type="checkbox" name="newsletter" value="newsletter" checked>i want to sign up   for newsletter<br>
                                      ^^^ remove square brackets here.
于 2013-11-10T19:17:31.773 回答
2
if(isset($_POST["newsletter"]) && $_POST["newsletter"] == "newsletter"){
    //checked
}
于 2013-11-10T19:18:04.897 回答
1

尝试改变这部分,

<input type="checkbox" name="newsletter[]" value="newsletter" checked>i want to sign up   for newsletter

为了这

<input type="checkbox" name="newsletter" value="newsletter" checked>i want to sign up   for newsletter
于 2013-11-10T19:20:04.833 回答
0

代替:

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

和:

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['tel'];
if (isset($_POST['newsletter'])) {
  $checkBoxValue = "yes";
} else {
  $checkBoxValue = "no";
}

然后替换这行代码:

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

和:

$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";
于 2013-11-10T19:23:54.613 回答