首页 > 解决方案 > 如何将复选框选择传递给 php mailer 表单?

问题描述

复选框字段没有从我的 html 表单传递到我的电子邮件。

当用户填写表单时,我的电子邮件中会收到所有信息(用户名、电子邮件、电话、评论),但他们在表单上选中的框除外。我尝试将 [] like this name="feature[]" 放在名称字段中,但没有奏效,所以我恢复了原状。

我的表格……

<form name='quote' method="post" action="quote-request.php">
    ...
            <center><h4 style='color:#C42D35'>Check each feature you will need:</h4></center>
    <div class='row'>
        <div class='col-md-6'>
            <input type="checkbox" name="feature" value="something" checked> Basic <br>
            <input type="checkbox" name="feature" value="something1"> Option 1 <br>
            <input type="checkbox" name="feature" value="something2"> Option 2 <br>    
    </div>
        <div class='col-md-6'>
            <input type="checkbox" name="feature" value="something3"> Option 3 <br>
            <input type="checkbox" name="feature" value="something4"> Option 4 <br>  
        </div>
    </div>
...
</form>

我的处理程序(quote-request.php)...

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

    $email_to = "myemail@mydomain.com";
    $email_subject = "Quote Request";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['firstName']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
        !isset($_POST['feature']) ||
        !isset($_POST['comment'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $firstName = $_POST['firstName']; // required
    $email_from = $_POST['email']; // required
    $phone = $_POST['phone']; // not required
    $feature = $_POST['feature']; // required
    $comment = $_POST['comment']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$firstName)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(strlen($comment) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "First Name: ".clean_string($firstName)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($phone)."\n";
    $email_message .= "Comments: ".clean_string($comment)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

}
?>

标签: phphtmlforms

解决方案


推荐阅读