I am having difficulties with the logic of sending to selected email addresses from a set of checkboxes.
I have a simple form with just a name and a surname inputs, and 4 checkboxes.
What I would like to do is when one of the checkboxes is checked or if multiple checkboxes are checked, to send the emails that those checkboxes represent. Depending of what checkboxes are checked to send to those specific emails only.
For example I have 4 emails, and if the first checkbox is checked to send to those emails. If checkbox 1 and checkbox 2 are checked to send to another emails or if checkboxes 2,3,4 are checked to send to another emails.
My code is working if I check only one checkbox.
If I check more than one, for example checkbox 1,3,4 it will send the emails only to the checkbox 1 emails, it will not proceed to the third one checkbox emails or the the fourth checkbox emails. I will ignore them.
Hopefully someone can help me solve this issue.
Thanks.
Here is my PHP code
<?php
use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";
if(isset($_POST['submit'], $_POST["cbone"])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->setFrom($email);
//$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
} else if(isset($_POST['submit'], $_POST['cbtwo'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbthree'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbfour'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbtwo'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbthree'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbfour'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbtwo'], $_POST['cbthree'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailthree@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbtwo'], $_POST['cbthree'], $_POST['cbfour'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbtwo'], $_POST['cbthree'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbtwo'], $_POST['cbfour'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbthree'], $_POST['cbfour'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else if(isset($_POST['submit'], $_POST['cbtwo'], $_POST['cbthree'], $_POST['cbfour'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p>
<p><strong>Surname:</strong> $surname</p>";
}else {
echo "Email was not send";
}
?>
Form Code
<form method="post" action="">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="text" name="surname" class="form-control" placeholder="Surname">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cbone" value="checkone" id="checkone">
<label class="form-check-label" for="checkone">
Checkbox One
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cbtwo" value="checktwo" id="checktwo">
<label class="form-check-label" for="checktwo">
Checkbox Two
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cbthree" value="checkthree" id="checkthree">
<label class="form-check-label" for="checkthree">
Checkbox Three
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="cbfour" value="checkfour" id="checkfour">
<label class="form-check-label" for="checkfour">
Checkbox Four
</label>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="Send Email">
</form>