【问题标题】:php form script to show checkbox group results in emailphp表单脚本在电子邮件中显示复选框组结果
【发布时间】:2016-06-30 06:34:33
【问题描述】:

我正在处理表单并使用 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>

这里是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;
  }
}

?> 

【问题讨论】:

    标签: php forms scripting


    【解决方案1】:

    $_POST['type_of_machine'] , $_POST['accessory_interest'] 是数组,因此即使使用 implode 方法 php 也无法从它们创建字符串,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> ";
            }
        }
    

    【讨论】:

    • implode 只会抛出一个数组到字符串的转换通知,如果你在数组中有另一个维度 imploded
    • 是的,你是对的,但是如果用户不选择任何复选框让我们说兴趣,那么表单将不会发送附件兴趣并且不设置 $_POST['accessory_interest'] 然后 php 会抛出警告:内爆() [function.implode]:传递的参数无效
    【解决方案2】:

    你有 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),您将了解您必须使用的值

    【讨论】:

    • 谢谢达贡。效果很好!我用了几天,我不知道如何使用 implode。
    • @CathyMarvin 欢迎您,请了解当有人在此处回答堆栈溢出时该怎么办:stackoverflow.com/help/someone-answers
    猜你喜欢
    • 2012-07-29
    • 2015-11-09
    • 2019-05-18
    • 2013-06-08
    • 2011-11-06
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多