php表单脚本显示复选框组结果在电子邮件中

时间:2016-03-15 00:47:47

标签: php forms scripting

我正在使用表单并使用php脚本来处理表单。一切正常,除了我试图在电子邮件中包含2个复选框组的值,它们在电子邮件中以数组的形式出现。

这是html:



<form method="post" action="form-to-email.php"  id="quickquoteform" name="quick quote" align="left" ><p ><input name="first_and_lastname" type="text" tabindex="1" required id="First and Last Name" placeholder="First and Last Name" title="First and Last Name"  size="50">
    </p>

    <p><input name="email" type="email" tabindex="2" required id="Email" placeholder="Email"  size="50"></p>
    <p><input name="phone" type="tel" tabindex="3" required id="Phone" placeholder="Phone" title="Phone" size="50"></p>
    <p><input name="town" type="text" tabindex="4" required id="Town" placeholder="Town" title="Town"  size="50"></p>
    <p><input name="lawn_size" type="text" tabindex="5" required id="Lawn Size" placeholder="Lawn Size" title="Lawn Size"  size="50"></p>
    <p><input name="current_tractor" type="text" tabindex="6" required id="Current Tractor" placeholder="Current Tractor" title="Current Tractor"  size="50"></p>
    <p><textarea name="comments" tabindex="7" cols="50" rows="12" maxlength="50" form="quickquoteform" placeholder="Comments"></textarea></p>
     
    <p>Are you interested in:
    <table width="300" class="formcheckbox" name="type_of_machine">
      <tr>
        <td><label>
          <input type="checkbox" tabindex="8" name="type_of_machine[]" value="ZTrak™ Mowers" id="TypeofMachine_0">
          ZTrak™ Mowers</label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" tabindex="9" name="type_of_machine[]" value="Compact Utility Mower" id="TypeofMachine_1">
          Compact Utility Mower</label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" tabindex="10" name="type_of_machine[]" value="Gator Utility Vehicles" id="TypeofMachine_2">
          Gator Utility Vehicle</label></td>
      </tr>
    </table>
    <p>
    <p>Would you like: 
    <table width="200" class="formcheckbox" name="accessory_interest">
      <tr>
        <td><label>
          <input type="checkbox" tabindex="11" name="accessory_interest[]" value="Bagger" id="Accessoryinterest_0">
          Bagger</label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" tabindex="12" name="accessory_interest[]" value="Snow Removal" id="Accessoryinterest_1">
          Snow Removal</label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" tabindex="13" name="accessory_interest[]" value="Bag Leaves" id="Accessoryinterest_2">
          Bag Leaves</label></td>
      </tr>
      <tr>
        <td><label>
          <input type="checkbox" tabindex="14" name="accessory_interest[]" value="Till Garden" id="Accessoryinterest_3">
          Till Garden</label></td>
      </tr>
    </table>
    </p>


    <input id="submit" type="submit" name="submit" value="SUBMIT FORM" class="stylesubmit"><input type="reset" id="reset" name="reset" value="Reset"/>

    <div class="g-recaptcha" data-sitekey="xxxx"></div>
     
    </form>
&#13;
&#13;
&#13;

这是php:

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['first_and_lastname'];
$visitor_email = $_POST['email'];
$message = $_POST['comments'];

//Validate first


if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
         $secretKey = "xxxx";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) 

if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}



$email_from = 'tim@website.com';//<== update the email address
$email_subject = "Quick Quote Form Submission";
$email_body = "You have received a new message from the user $first_and_lastname.\n".
    "Here is the message:\n $comments".

$email_body = "<html>";
   $email_body .= "<h2>Quick Quote</h2>";
   $email_body .= "<b>Name:</b> " .$_POST['first_and_lastname'];
   $email_body .= "<br><b>Email:</b> ".$_POST['email'];
   $email_body .= "<br><b>Phone:</b> " .$_POST['phone'];
   $email_body .= "<br><b>Town:</b> ".$_POST['town'];
   $email_body .= "<br><b>Lawn size:</b> ".$_POST['lawn_size'];
   $email_body .= "<br><b>Current Tractor:</b> ".$_POST['current_tractor'];
   $email_body .= "<br><b>Comments:</b> ".$_POST['comments'];
   $email_body .= "<br><b>Type of machine interested in:</b> ".$_POST['type_of_machine'];
   $email_body .= "<br><b>Type of accessory interested in:</b> ".$_POST['accessory_interest'];

   $email_body .= "</html>";    

$to = "webmster@website.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-Mailer: PHP / ".phpversion()."\n";
$headers .= "Return-Path: <$email>\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: confirmation.html');







// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

2 个答案:

答案 0 :(得分:0)

你有2个多维数组来处理;我建议implode列出所有选中的值:

 $email_body .= "<br><b>Type of machine interested in:</b> ".implode(' ',$_POST['type_of_machine']);
$email_body .= "<br><b>Type of accessory interested in:</b> ".implode(' ',$_POST['accessory_interest']);

以上我使用空格作为条目之间的粘合剂;但如果您愿意,可以使用一些HTML

如果您运行print_r($_POST),您将了解必须使用的值

答案 1 :(得分:0)

$ _ POST ['type_of_machine'],$ _POST ['accessory_interest']是数组,所以即使使用implode方法,你也无法从它们创建字符串php将数组转换为字符串转换通知。您可以更改此部分

   $email_body .= "<br><b>Type of machine interested in:</b> ".$_POST['type_of_machine'];
   $email_body .= "<br><b>Type of accessory interested in:</b> ".$_POST['accessory_interest'];
像这样

if ( isset($_POST['type_of_machine']) ){
    $email_body .= "<br><b>Type of machine interested in:</b>";
    foreach (   $_POST['type_of_machine'] as $k => $v  ) { 
            $email_body .= $v . "</b>";
        }
    }
if ( isset($_POST['accessory_interest']) ){
    $email_body .= "<br><b>Type of accessory interested in:</b> ";
    foreach (   $_POST['accessory_interest'] as $k => $v  ) { 
            $email_body .= $v . "</b> ";
        }
    }