如何在 PHPmailer 中显示所有选中的复选框

梅尔辛巴库

我想创建一个复选框并获取所有选中的复选框并使用 phpmailer 将它们通过电子邮件发送到我的电子邮件。显然,我只能从所有选定的复选框中选择 1 个值。对不起,如果我从这些缩进中让你的大脑自杀了 xd 这是我的 html 文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Send Mail</title>
</head>
<body>
<h3> Send Email using SMTP </h3>
    <form action="mail.php" method="post">
<label for="cfselection-0">
      <input name="cfselection" id="cfselection-0" value="website design type="checkbox">
      Website Design
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-1">
      <input name="cfselection" id="cfselection-1" value="search engines" type="checkbox">
      Search Engines and Ranking
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-2">
      <input name="cfselection" id="cfselection-2" value="media marketing" type="checkbox">
      Social Media Marketing and Campaigns
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-3">
      <input name="cfselection" id="cfselection-3" value="other" type="checkbox">
      Other
    </label>
    </div>
  </div>
</div>
        <button type="submit"> Dërgo </button>
    </form>


</body>
</html>

这是我的 php 文件:

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//get data from form
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$checkbox = $_POST['cfselection'];
// preparing mail content
$messagecontent ="Name = ". $name . "<br>Email = " . $email . "<br>Message =" . $message . "<br>Checkbox =" . $checkbox;


//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '[email protected]';                     //SMTP username
    $mail->Password   = 'xxxxxxxxxxxxxxx';                               //SMTP password
    $mail->SMTPSecure = "tls";            //Enable implicit TLS encryption
    $mail->Port       = 587;

    //Recipients
    $mail->setFrom('[email protected]');
    $mail->addAddress('[email protected]' );     //Add a recipient
    //$mail->addAddress('[email protected]');               //Name is optional
    //$mail->addReplyTo('[email protected]', 'Information');
    //$mail->addCC('[email protected]');
    //$mail->addBCC('[email protected]');

    //Attachments

    //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
   // $mail->addAttachment('photo.jpeg', 'photo.jpeg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = "blabla";
    $mail->Body    = $messagecontent;
    

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    // echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
艾迪森

您需要name在表单上的复选框属性中使用数组语法:

name="cfselection[]"

这将允许以相同的名称提交多个值。(如果不这样做,只需单独提交最后一个,其他的都会被覆盖。)

然后在 PHP 中,您将拥有一个数组$_POST["checkbox"],您可以在其中处理以查看所有选定的项目。

对于您的情况,将其放入电子邮件的最简单方法可能是将数组内为逗号分隔的字符串:

$checkbox = implode(", ", $_POST['cfselection']);

因此,例如,如果我勾选“搜索引擎和排名”和“社交媒体营销和活动”并提交表单,value每个相关复选框$_POST['cfselection']中的电子邮件将是:

search engines, media marketing

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何选中checkedListBox wpfToolkit中的所有复选框

如果未选中recyclerview中的所有复选框,如何显示吐司?

如何在反应钩子中以编程方式取消选中复选框列表中的所有复选框?

如何在DataGridView中取消选择所有选中的复选框

如何在剃须刀中获取所有选中的复选框

如何在WPF中获得所有选中的复选框?

如何在Laravel的多选复选框列表中显示选中的复选框?

选中所有子复选框后,如何选中“全选”复选框?

ASP.NET MVC Razor如何显示表中已选中的所有复选框的数量

如何选中和取消选中div中的所有复选框

如何在单个复选框事件中选中/取消选中所有asp.net复选框?

如何在Vuetify中选中一个复选框时选中所有复选框?

当不再选中过滤器复选框时,如何再次显示所有 div?

如何仅从组复选框中的另一个复选框中选中所有其他复选框

如何获取所有选中的复选框

如何检查所有复选框是否都未选中

如何打印所有选中的复选框?

如果所有子复选框都被选中,如何检查父复选框

如果在本机中未选中所有复选框,如何禁用按钮

如何检查PHP中是否已选中所有复选框?

如何检查 Reactjs 中是否选中了所有复选框?

如何将所有选中的复选框类名称存储在数组中?

如何实现Jquery复选框选中全部/取消选中带有复选框的所有功能?

如何在角度为4/6/7的表单提交中传递所有选中的复选框值

取消选中一个复选框时如何取消选中以下所有复选框

如果取消选中所有子复选框,如何取消选中父复选框,反之亦然?

未选中所有其他复选框时如何设置始终选中的1个复选框

67个动态复选框,如何在选中4个复选框后禁用所有复选框?

当我选中所有复选框时如何选择所有复选框