如何将复选框选择传递给php mailer表单?

时间:2019-05-13 23:56:34

标签: php html forms

复选框字段不会从我的html表单传递到我的电子邮件中。

当用户填写表单时,除他们在表单上选中的框外,所有信息都会通过我的电子邮件(用户名,电子邮件,电话,评论)接收。 我尝试将[]这样的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

}
?>

0 个答案:

没有答案