mercredi 11 août 2021

Add checkbox value to php mail

I have copied a form from the internet and tried to modify it to my needs. I added some checkboxes, and now I need the checkbox result to be displayed in the email that gets sent. The problem is, I don't really know php and currently every checkbox value gets sent in the email no matter what is selected. I would need it to only send the value of the selected checkboxes. I just tried to mimic how it was done with the text fields, but it doesn't work that way.

HTML

<p><label><input type="checkbox" value="2½-Zimmer-Wohnung" name="interessea">2½-Zimmer-Wohnung</label></p>
<p><label><input type="checkbox" value="3½-Zimmer-Wohnung" name="interesseb">3½-Zimmer-Wohnung</label></p>
<p><label><input type="checkbox" value="4½-Zimmer-Wohnung" name="interessec">4½-Zimmer-Wohnung</label></p>

PHP

<?php
if($_POST)
{
require('config.php');
    
    $user_name      = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
    $user_vorname   = filter_var($_POST["vorname"], FILTER_SANITIZE_STRING);
    $user_email     = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $user_phone     = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
    $user_strasse   = filter_var($_POST["strasse"], FILTER_SANITIZE_STRING);
    $user_plz       = filter_var($_POST["plz"], FILTER_SANITIZE_STRING);
    $content        = filter_var($_POST["content"], FILTER_SANITIZE_STRING);
    $interessea   = filter_var($_POST["interessea"], FILTER_SANITIZE_STRING);
    $interesseb   = filter_var($_POST["interesseb"], FILTER_SANITIZE_STRING);
    $interessec   = filter_var($_POST["interessec"], FILTER_SANITIZE_STRING);
    
    
    if(empty($user_name)) {
        $empty[] = "<b>Name</b>";       
    }
    if(empty($user_vorname)) {
        $empty[] = "<b>Vorname</b>";
    }
        if(empty($user_strasse)) {
        $empty[] = "<b>Strasse</b>";        
    }
        if(empty($user_plz)) {
        $empty[] = "<b>PLZ/Ort</b>";        
    }
    if(empty($user_email)) {
        $empty[] = "<b>Email</b>";
    }
    if(empty($user_phone)) {
        $empty[] = "<b>Telefonnummer</b>";
    }   
    
    
    if(!empty($empty)) {
        $output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' benötigt!'));
        die($output);
    }
    
    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
        $output = json_encode(array('type'=>'error', 'text' => '<b>'.$user_email.'</b> is an invalid Email, please correct it.'));
        die($output);
    }
    
    //reCAPTCHA validation
    if (isset($_POST['g-recaptcha-response'])) {
        
        require('component/recaptcha/src/autoload.php');        
        
        $recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY);

        $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

          if (!$resp->isSuccess()) {
                $output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
                die($output);               
          } 
    }
    var_dump($_POST);
    $toEmail = "ss@admotion.ch";
    $mailHeaders = "From: " . $user_name . "<" . $user_email . ">\r\n";
    $mailBody .= "Name: " . $user_name . "\n";
    $mailBody .= "Vorname: " . $user_vorname . "\n";
    $mailBody .= "Strasse: " . $user_strasse . "\n";
    $mailBody .= "PLZ/Ort: " . $user_plz . "\n";
    $mailBody .= "Email: " . $user_email . "\n";
    $mailBody .= "Telefon: " . $user_phone . "\n";
    $mailBody .= "Mitteilung: " . $content . "\n";
    $mailBody .= "Ich bin interessiert an einer: " . $interessea . $interesseb . $interessec . "\n";

    if (mail($toEmail, "Interesse Beatus", $mailBody, $mailHeaders)) {
        $output = json_encode(array('type'=>'message', 'text' => 'Vielen Dank für Ihr interesse. Wir werlden uns so schnell wie möglich bei Ihnen melden.'));
        die($output);
    } else {
        $output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));
        die($output);
    }
}
?>



Aucun commentaire:

Enregistrer un commentaire