I've taken your code and been trying to add to it, specifically adding checkboxes in an array. However, I am still coming up with an undefined variable if the checkboxes are skipped.
Any chance you can help with this? I am sure I'm not the only person who's said "it would be nice to have checkboxes in a form"
class Ajax_Form {
# Constants to redefined
# Check this for more configurations: https://blog.mailtrap.io/phpmailer
const HOST = 'mail.bluehonudesign.com'; # SMTP server
const USERNAME = '*******@bluehonudesign.com'; # SMTP username
const PASSWORD = '*****'; # SMTP password
const SECRET_KEY = '6LcKHswiAAAAACS-R11KgDa92U76S1LVQPCbsGBc'; # GOOGLE secret key
const SMTP_SECURE = PHPMailer::ENCRYPTION_STARTTLS;
const SMTP_AUTH = true;
const PORT = 587;
const SUBJECT = 'Contact Form Message';
const HANDLER_MSG = [
'success' => '<strong>βοΈ Your message has been sent!</strong><br>
We will be sure to return your message shortly. Thanks!',
'token-error' => 'β Error recaptcha token.',
'enter_name' => 'β Please enter your name.',
'enter_email' => 'β Please enter a valid email.',
'enter_message' => 'β Please enter your message.',
'enter_phone' => 'β Please enter your phone number.',
'bad_ip' => 'β 56k ?',
'ajax_only' => 'β Asynchronous anonymous.',
'email_body' => '
<style>
body {
font-family: arial, helvetica, sans-serif !important;
}
table {
width: 100%;
margin: 20px auto;
table-layout: auto;
}
.fixed {
table-layout: fixed;
}
table, td, th {
border-collapse: collapse;
}
th, td {
padding: 10px;
border: none;
border-bottom: 1px dotted #ccc;
text-align: left;
vertical-align: top;
font-size: 1rem;
}
td:first-child {
width: 100px;
}
</style>
<table>
<tr><td colspan="2"><h1>{{subject}}</h1></td></tr>
<tr><td><b>Date</b>:</td><td>{{date}}</td></tr>
<tr><td><b>Name</b>:</td><td>{{name}}</td></tr>
<tr><td><b>E-Mail</b>:</td><td>{{email}}</td></tr>
<tr><td><b>Phone</b>:</td><td>{{phone}}</td></tr>
<tr><td><b>Best time to call</b>:</td><td>{{when}}</td></tr>
<tr><td colspan="2"><br></td></tr>
<tr><td><b>Current website</b>:</td><td>{{website}}</td></tr>
<tr><td><b>Interested In</b>:</td><td>{{service}}</td></tr>
<tr><td colspan="2"><br></td></tr>
<tr><td><b>Address</b>:</td><td>{{address1}}<br>
{{address2}}<br>
{{city}}, {{state}} {{zip}}</td></tr>
<tr><td colspan="2"><br></td></tr>
<tr><td><b>Referring source</b>:</td><td>{{howfind}}</td></tr>
<tr><td><b>Message</b>:</td><td>{{message}}</td></tr>
<tr><td><b>IP</b>:</td><td>{{ip}}</td></tr>
</table>
'
];
/**
* Ajax_Form constructor
*/
public function __construct()
{
# Check if request is Ajax request
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest') {
$this->statusHandler('ajax_only');
}
# Check if fields has been entered and valid
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $this->secure($_POST['name']) ?? $this->statusHandler('enter_name');
$email = filter_var($this->secure($_POST['email']), FILTER_SANITIZE_EMAIL) ?? $this->statusHandler('enter_email');
$message = $this->secure($_POST['message']) ?? $this->statusHandler('enter_message');
$phone = $this->secure($_POST['phone']) ?? $this->statusHandler('enter_phone');
$token = $this->secure($_POST['recaptcha-token']) ?? $this->statusHandler('token-error');
$ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) ?? $this->statusHandler('bad_ip');
$date = new DateTime();
$address1 = $this->secure($_POST['address1']) ?? $this->statusHandler('enter_address1');
$address2 = $this->secure($_POST['address2']);
$city = $this->secure($_POST['city']);
$state = filter_input(INPUT_POST, 'state', FILTER_UNSAFE_RAW);
$zip = $this->secure($_POST['zip']);
$when = filter_input(INPUT_POST, 'when', FILTER_UNSAFE_RAW);
if (isset($_POST['howfind'])) {
$howfind = implode(',', $_POST['howfind']);
} else
if (empty(['howfind']))
echo"foo";
if (isset($_POST['service'])) {
$service = implode(',', $_POST['service']);
} else
if (empty(['service']))
echo"foo";
$website = $this->secure($_POST['website']);
};
# Prepare email body
$email_body = self::HANDLER_MSG['email_body'];
$email_body = $this->template($email_body, [
'subject' => self::SUBJECT,
'date' => $date->format('j/m/Y H:i:s'),
'name' => $name,
'phone' => $phone,
'email' => $email,
'ip' => $ip,
'address1' => $address1,
'address2' => $address2,
'city' => $city,
'state' => $state,
'zip' => $zip,
'message' => $message,
'website' => $website,
'howfind' => $howfind,
'service' => $service,
'when' => $when,
]);
# Verifying the user's response
$recaptcha = new \ReCaptcha\ReCaptcha(self::SECRET_KEY);
$resp = $recaptcha
->setExpectedHostname($_SERVER['SERVER_NAME'])
->verify($token, $_SERVER['REMOTE_ADDR']);
if ($resp->isSuccess()) {
# Instanciation of PHPMailer
$mail = new PHPMailer(true);
$mail->setLanguage('en', __DIR__ . '/vendor/PHPMailer/language/');
try {
# Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF; # Enable verbose debug output
$mail->isSMTP(); # Set mailer to use SMTP
$mail->Host = self::HOST; # Specify main and backup SMTP servers
$mail->SMTPAuth = self::SMTP_AUTH; # Enable SMTP authentication
$mail->Username = self::USERNAME; # SMTP username
$mail->Password = self::PASSWORD; # SMTP password
$mail->SMTPSecure = self::SMTP_SECURE; # Enable TLS encryption, `ssl` also accepted
$mail->Port = self::PORT; # TCP port
# Recipients
# $mail->setFrom(self::USERNAME, $name);
# $mail->addAddress("[email protected]");
#$mail->addAddress($email, $name);
# $mail->AddCC(self::USERNAME, 'Dev_copy');
# $mail->addReplyTo(self::USERNAME, $email);
$mail->setFrom($email, $name);
$mail->addAddress('[email protected]');
# $mail->AddCC(self::USERNAME);
$mail->addReplyTo($email, $name);
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
# Content
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = self::SUBJECT;
$mail->Body = $email_body;
$mail->AltBody = strip_tags($email_body);
# Send email
$mail->send();
$this->statusHandler('success');
} catch (Exception $e) {
die(json_encode($mail->ErrorInfo));
}
} else {
die(json_encode($resp->getErrorCodes()));
}
}
}