使用复选框选择联系人表单中的电子邮件地址

时间:2013-07-16 08:52:07

标签: php checkbox contact-form

我已经创建了一个电子邮件表单,根据选中的复选框,该表单应该发送到两个不同的电子邮件地址。但是我试图让它发挥作用真的很麻烦,让我告诉你我想要做什么。

请注意,表单实际上效果很好,只是通过复选框发送电子邮件给我失败。

请帮助!

这是表格部分:

<div id="respond">
            <?php echo $response; ?>
            <form id="commentForm" method="post" action="#contact2">

              <div>
            <small>Name:</small>
                <input type="text" name="message_name" placeholder="Enter you name..." value="<?php echo $_POST['message_name']; ?>">
            <small>Department:</small>
<!-- EDIT || replaced type=checkbox with type=radio -->
                <input type="radio" name="sales" value="sales@h-s-c.co.uk" checked/>
                <input type="radio"name="lettings" value="lettings@h-s-c.co.uk" />
            <small>Email:</small>
                <input type="text" name="message_email" placeholder="somebody@email.co.uk" value="<?php echo $_POST['message_email']; ?>">
            <small>Phone:</small>    
                <input class="" type="tel" name="phone_number" placeholder="07912 208 936" value="<?php echo $_POST['phone_number']; ?>">
              </div>

              <div style="float:right;">
            <small>Message:</small>
                <textarea type="text" name="message_text" placeholder="I would like further information on..."><?php echo $_POST['message_text']; ?></textarea>
               </div>

            <small>Human Verification:</small><br>
                <input type="text" style="width: 60px;" name="message_human" placeholder="2"> + 3 = 5</label>
                 <input type="hidden" name="submitted" value="1">
                <input class="" type="submit" value="submit"/>

            </form>
           </div>

然后发送表单的php看起来像这样:

    <?php
    //Include email and to name
    $admin_email = $_POST[$email_address];//EDIT || changed to new value $email_address
    $block = "Test";

//Remove this section
//foreach($_POST['check'] as $value) {
  //  $checkbox .= "$value\n";
//} 
    ?>
    <?php

      //response generation function

      $response = "";

      //function to generate response
      function generate_response($type, $message){

        global $response;

        if($type == "success") $response = "<div class='success'>{$message}</div>";
        else $response = "<div class='error'>{$message}</div>";

      }

      //response messages
      $not_human       = "Human verification incorrect.";
      $missing_content = "Please supply all information.";
      $email_invalid   = "Email Address Invalid.";
      $message_unsent  = "Message was not sent. Try Again.";
      $message_sent    = "Thanks! Your message has been sent.";

      //user posted variables
      $name = $_POST['message_name'];
      $email = $_POST['message_email'];
      $phone = $_POST['phone_number'];
      $message = $_POST['message_text'];
      $human = $_POST['message_human'];

      //php mailer variables
      $to = $admin_email;
      $subject = "Someone sent a message from {$block}";
      $headers = 'From: '. $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n";
        $tel = 'Contact no: ' . $phone;

      if(!$human == 0){
        if($human != 2) generate_response("error", $not_human); //not human!
        else {

          //validate email
          if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            generate_response("error", $email_invalid);
          else //email is valid
          {
            //validate presence of name and message
            if(empty($name) || empty($message)){
              generate_response("error", $missing_content);
            }
            else //ready to go!
            {
              $sent = mail($to, $subject, $message . $tel , $headers);
              if($sent) generate_response("success", $message_sent); //message sent!
              else generate_response("error", $message_unsent); //message wasn't sent
            }
          }
        }
      } 
      else if ($_POST['submitted']) generate_response("error", $missing_content);

    ?>

4 个答案:

答案 0 :(得分:0)

提交表单时,复选框不会向服务器提供其值。如果选中复选框,则复选框的值仅为on。请改用radiobuttonselect box

答案 1 :(得分:0)

Checkbox不发送字符串值,只是复选框标识符的布尔值。

在代码中添加新条件 如果选中,则使用其他复选框的电子邮件空白。

答案 2 :(得分:0)

<?php
//Include email and to name
$admin_email = $_POST[$checkbox];  // no element with name 'checkbox' 
$block = "Test";

// no element with name 'check'
foreach($_POST['check'] as $value) {
$checkbox .= "$value\n";
} 
?>


these are main issue.

If you want to send mail to any one from the two options then use select, and in value of     option provide email id

//HTML CODE
<select name="to_email">
<option value="sales@xxxx">Sales</option>
<option value="lettings@xxxx">Lettings</option>
</select>


PHP CODE

<?php
$admin_email = $_POST['to_email'];  // this will get you the selected email id
?>

let me know if you have any queries.

答案 3 :(得分:0)

*<form id="requestform" name="requestform" action="enquiry.php" class="col-xs-12" method="post">
            <div class="form-wrapper">
                <div class="col-xs-12">
                    <div class="field col-xs-6">
                        <label class="formlabel"> Email </label> 
                        <input class="textfield"  data-required="true" type="email" name="email" />
                    </div>
                    <div class="field col-xs-6">
                        <label class="formlabel"> Phone </label>
                        <input class="textfield"  data-required="true" type="text" name="phone" />
                    </div>
                </div>
                <div class="clear"></div>
                <div class="col-xs-12">                 
                    <div class="field col-xs-6">
                        <label class="formlabel"> Details </label>
                        <textarea class="textfield textarea" type="textarea"  data-required="true" name="details"/></textarea >
                    </div>
                    <div class="field emailfunc col-xs-6">
                        <label class="formlabel"> What can we help you with ? </label>
                        <span>
                            <input name="interested" id="logistics" value="logistics" onchange="" type="checkbox">
                            <label class="inline" >Logistics</label>
                        </span>
                        <span>
                            <input name="interested" id="sales" value="sales" onchange="" type="checkbox">
                            <label class="inline" >Sales</label>
                        </span>
                        <span>
                            <input name="interested" id="customer" value="customer" onchange="" type="checkbox">
                            <label class="inline" >Customer</label>
                        </span>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="clear"></div>
            </div>

            <div class="col-xs-12">                 
                <div class="field col-xs-6">
                    <input class="submit" type="submit" value="<?php echo Mage::helper('contacts')->__('Send Enquiry') ?>" name="submit">
                </div>
            </div>
            <div class="clear"></div>

        </form>


<?php
    //enquiry.php
    $email=$_REQUEST['email'];
    $phone=$_REQUEST['phone'];
    $details=$_REQUEST['details'];
    $interested=$_REQUEST['interested'];

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "From: \"Request a Quote\" <fakhruddinsouq>\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $message = '<html><head><title>Fakhruddin Souq</title></head><body>
    <div style="color:#006A93; width:auto; height:auto; background-color:#eeeeee; margin:0px auto;">
  <fieldset style=" border-color:#006A93; border-style:double;">
    <legend align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; text-decoration:underline;">Request a Quote</legend>
    <head><title>Fakhruddin Souq</title></head>
    <table border="0" cellpadding="0" cellspacing="4" align="left" width="100%" height="100%" style="color:#006A93; font-weight:bold; font-size:12px; font-family:Arial, Helvetica, sans-serif;">
        <td align="left" valign="top">Email: </td>
        <td align="left" valign="top">'.$email.'</td>
      </tr>
       <tr>
        <td align="left" valign="top">Phone: </td>
        <td align="left" valign="top">'.$phone.'</td>
      </tr>
      <tr>
        <td align="left" valign="top"> Product Details: </td>
        <td align="left" valign="top">'.$details.'</td>
      </tr>
      <tr>
        <td colspan="2" align="left" valign="top" style="font-size:14px;">Regards</td>
      </tr>
    </table>
    <span style="clear:both; float:left; font-size:14px; padding-left:4px; padding-top:10px; font-family:Arial, Helvetica, sans-serif;">Posted Date & Time:'.date('m/d/Y H:i:s').'</span>
  </fieldset>
</div>
        </body>
        </html>';

    if($interested == 'logistics') {
        $to = "abc@email.com";
    }
    else if($interested == 'sales') {
        $to = "def@email.com"; 
    } 
    else if($interested == 'customer') {
        $to = "imemail@email.com"; 
    }
    else if($interested == 'logistics' && $interested == 'sales' && $interested == 'customer'){
        $to = "abc@email.com,def@email.com,imemail@email.com"; 
    }
    else if($interested == 'logistics' && $interested == 'sales'){
        $to = "abc@email.com,def@email.com"; 
    }
    else if($interested == 'logistics' && $interested == 'customer') {
        $to = "abc@email.com,imemail@email.com"; 
    }
    else if($interested == 'sales' && $interested == 'customer') {
        $to = "def@email.com,imemail@email.com"; 
    }
    else {
        $to ="imemail@email.com"; 
    }

    if (mail($to,'Mail From '.$name,$message,$headers)){       
        print json_encode(array('type'=>'done', 'text' => 'Thank You For Your Enquiry'));
        exit;
    }
    else {       
        print json_encode(array('type'=>'done', 'text' => 'Sorry Email is not sent'));
        exit;
    } 
?>*
相关问题