jeudi 25 juin 2020

Multiple checkbox value in PHPMailer displays empty [duplicate]

I have set up PHP mailer to deliver contact form information on my website. I am having trouble sorting out the multiple checkbox inputs. It appears empty in emails. Here the form HTML code:

<div class="form-group">
            <label class="checkbox-inline">
                <input name="time" type="checkbox" id="inlineCheckbox1" value="AM"> AM
            </label>
            <label class="checkbox-inline">
                <input name="time" type="checkbox" id="inlineCheckbox2" value="PM"> PM
            </label>
        </div>

In my PHP code, I added below to check if there are inputs for the checkboxes:

if($_POST["time"])  {
        $time_array = $_POST['time'];
        $time_tostring = implode(", ", $time_array); 
    }

Then in the mail code, I added this:

$mail = new PHPMailer(true);
try {
    //Server settings
    $mail->isSMTP();
    $mail->Host = $config['host'];
    $mail->SMTPAuth = true;
    $mail->Username = $config['username'];
    $mail->Password = $config['password'];
    $mail->SMTPSecure = $config['secure'];
    $mail->Port = $config['port'];

    //Recipients
    $mail->setFrom($config['from'], $config['fromName']);
    $mail->addAddress($config['sendTo']);
    //$mail->addCC($config['sendToCC']);            
    $mail->addBCC($config['sendToBCC']);            

    //Content
    $mail->isHTML(true);
    $mail->Subject = 'Website Contact Form';
    $mail->Body    = '<p>Emptied on: ' . $_POST['day'] . "</p>"
    . "<p>Time: "  . $time_tostring . "</p>"        
    . "<p>Name: " . $_POST['name'] . "</p>"
    . "<p>Address: " . $_POST['address'] . "</p>"
    . "<p>Phone Number: " . $_POST['number'] . "</p>"
    . "<p>Email: " . $_POST['email'] . "</p>"
    . "<p>Where did you hear about us: " . $_POST['how'] . "</p>"
    . "<p>Service interested in: " . $_POST['comments'] . "</p>"        
    ;

    $mail->send();
    echo json_encode(['status' => true, "data" => 'Message has been sent']);
} catch (Exception $e) {
    echo json_encode(['status' => false, "data" => "Message could not be sent\nMailer Error: " . $mail->ErrorInfo]);
}

In the email sent, the time is empty and do not display any value.

I also tried the line below directly but it didn't work too.

. "<p>Time: "  . implode(', ', $_POST['time']) . "</p>" 

I am not sure why this is not working. Any idea is appreciate.




Aucun commentaire:

Enregistrer un commentaire