添加复选框到我的PHP邮件

时间:2015-03-26 15:46:44

标签: php html checkbox

我已经联系了一份表格。 在HTML中我也做了两个复选框。我已经在我的php表单中添加了这些复选框,但是当我收到电子邮件(并且我已经检查了一个或两个盒子)时,它们没有显示。我错过了什么?

这是HTML

<input id="cb01" name="afspraak" type="checkbox" value="ja">
<input id="cb02" name="nieuwsbrief" type="checkbox" value="ja">

这是PHP表单

<?php


$siteOwnersEmail = 'email@gmail.com';



if($_POST) {

   $fname = trim(stripslashes($_POST['contactFname']));
   $lname = trim(stripslashes($_POST['contactLname']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check First Name
    if (strlen($fname) < 2) {
        $error['fname'] = "U bent uw naam vergeten in te vullen";
    }

   // Check Afspraak
    if (strlen($subject) < 1) {
        $error['fname'] = "Wilt u een afspraak maken? Een 'ja' of 'nee' volstaat.";
    }

    // Check Email
    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "U dient een kloppend e-mailadres in te voeren";
    }
   // Subject
    if ($subject == '') { $subject = "Contact Form Submission"; }


   // Set Message
   $message .= "Email van: " . $fname . "<br />";
   $message .= "Telefoonnummer: " . $lname . "<br />";
   $message .= "Emailadres: " . $email . "<br />";
   $message .= "Afspraak maken? " . $subject . "<br />";

   $message .= $_POST['afspraak'] == "ja" ? "afspraak: Ja \n" : "";
   $message .= $_POST['nieuwsbrief'] == "ja" ? "nieuwsbrief: Ja \n" : "";

   $message .= "Bericht: <br />";   
   $message .= $contact_message;
   $message .= "<br /> ----- <br /> Dit bericht is verstuurd met het contactformulier op de website <br />";

   // Set From: header
   $from =  $name . " <" . $email . ">";

   // Email Headers
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


   if (!$error) {

      ini_set("sendmail_from", $siteOwnersEmail); // for windows server
      $mail = mail($siteOwnersEmail, $subject, $message, $headers);

        if ($mail) { echo "OK"; }
      else { echo "Er is iets niet goed gegaan! Probeer het opnieuw."; }

    } # end if - no validation error

    else {

        $response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
        $response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

        echo $response;

    } # end if - there was a validation error

}


?>

1 个答案:

答案 0 :(得分:0)

见下面更新的代码:

$message .= ($_POST['afspraak'] === "ja") ? "afspraak: Ja \n" : "";
$message .= ($_POST['nieuwsbrief'] === "ja") ? "nieuwsbrief: Ja \n" : "";

*请记住,在提交页面之前选中复选框。