Home > Enterprise >  How to display all selected Checkboxes in PHPmailer
How to display all selected Checkboxes in PHPmailer

Time:04-03

I was tying to create a checkbox and get all checkboxes that were selected and ues phpmailer to email them to my email. Apparently i can get only 1 value selected from checkboxes out of all selected ones. Sorry if i make ur brain suicide from these indents xd This is my html file :

<!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 >
    <label for="cfselection-1">
      <input name="cfselection" id="cfselection-1" value="search engines" type="checkbox">
      Search Engines and Ranking
    </label>
    </div>
  <div >
    <label for="cfselection-2">
      <input name="cfselection" id="cfselection-2" value="media marketing" type="checkbox">
      Social Media Marketing and Campaigns
    </label>
    </div>
  <div >
    <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>

and this is my php file :

<?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}";
}

CodePudding user response:

You need to use array syntax in your checkboxes' name attributes on the form:

name="cfselection[]"

This will allow multiple values to be submitted under the same name. (If you don't, simply the last one is submitted alone, the others are overwritten.)

Then in PHP, you'll have an array in $_POST["checkbox"] which you can process to see all the selected items.

For your case probably the simplest way to get that into the email is to implode the array into a comma-separated string:

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

So for example if I ticked "Search Engines and Ranking" and "Social Media Marketing and Campaigns" and submitted the form, the value from each of the associated checkboxes would go into the $_POST['cfselection'] variable, and the result of imploding that which you'd see in the email would be:

search engines, media marketing
  • Related