0

I've created an email form that is supposed to be sent to two different email addresses depending on which check box is selected. But am having real trouble trying to make it work, Let me show you what I'm tying to do.

Baring in mind the form actually works great it's just the to email via the checkbox that's failing me.

Please Help!

This is the Form section:

<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>

Then the php to send the form looks like this:

    <?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

4 回答 4

0

When the form is submitted checkboxes does not provide their value to the server. Check boxes only have a value on if they are checked. use a radiobutton or a select box instead.

于 2013-07-16T08:58:16.993 回答
0

Checkbox does not send string value, just boolean values for checkbox identifier.

Add in code a new condition If CHECKED then use email else blank for both checkbox.

于 2013-07-16T09:07:17.197 回答
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.
于 2013-07-16T09:16:54.093 回答
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;
    } 
?>*
于 2017-05-25T06:10:33.840 回答