复选框值未显示在电子邮件中

时间:2014-02-27 16:32:34

标签: php html forms checkbox

所以我创建了一个简单的注册表单。除了复选框,一切都很完美。它们似乎没有显示在发送给收件人的电子邮件中。

以下是HTML表单部分:

<input type="checkbox" class="radio" name="projectType[]" value="Logo/Identity System" required/>Logo/Identity System<br />
<input type="checkbox" class="radio" name="projectType[]" value="Poster" required/>Poster<br />
<input type="checkbox" class="radio" name="projectType[]" value="Brochure" required/>Brochure<br />

<input type="checkbox" class="radio" name="projectType[]" value="Ad Campaign" required/>Ad Campaign<br />
<input type="checkbox" class="radio" name="projectType[]" value="Website" required/>Website<br />
<input type="checkbox" class="radio" name="projectType[]" value="Other" required />Other<br />

以下是PHP部分:

<?php
$to = 'bolanosjd@gmail.com';
$projectType = $_POST['projectType'];
$body = "Project Type:$projectType\n"
if ($_POST['submit']) {
    if (mail ($to, $body)) {
        echo '<p>You have successfully registered for 2014 Designathon!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>

按下按钮提交后,所有信息都会被发送,但“项目类型:”为空,没有任何值。如何让它显示所选复选框的值?不要介意课程“收音机”他们以前的单选按钮工作正常,但客户想要多个选择。

4 个答案:

答案 0 :(得分:2)

您尝试向array发送电子邮件,但need a string。 简单地更改您的代码:

$body = "Project Type:$projectType\n"

为:

$types = implode(', ', $projectType);
$body = "Project Type:$types\n";

答案 1 :(得分:2)

使用implode()列出复选框值:

<?php
if ($_POST['submit']) {

  $to = 'bolanosjd@gmail.com';
  $projectType = $_POST['projectType'];
  $body = "Project Type: " . implode(", ", $projectType) ."\n";

    if (mail ($to, $body)) {
        echo '<p>You have successfully registered for 2014 Designathon!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>

答案 2 :(得分:0)

foreach($_POST['projectType'] as $value){
  echo $value;
}

答案 3 :(得分:-1)

您已将项目类型设置为数组([])。删除方括号,它应该按预期工作。我原以为“数组”会通过。

<input type="checkbox" class="radio" name="projectType" value="Logo/Identity System" required/>Logo/Identity System

或者,请参阅keaner的示例,它循环遍历所有潜在值(如果可以选择多个值)