<?php
# Email ]lidation and Sending
$errors = [];
$missing = [];
// check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// email processing script
$to = 'robert@nwtc-web.com';
$subject = 'Feedback from Japan Journey';
// list expected fields
$expected = ['name', 'email', 'comments'];
// set required fields
$required = ['name', 'email', 'comments'];
# Not loading via include - showing here for demo only...
// require('processmail_05.php');
// pattern to locate suspect phrases
$pattern = '/[\s\r\n]|Content-Type:|Bcc:|Cc:/i';
// check the submitted email address
$suspect = preg_match($pattern, $_POST['email']);
if (!$suspect) {
foreach ($_POST as $key => $value) {
// strip whitespace from $value if not an array
if (!is_array($value)) {
$value = trim($value);
}
if (!in_array($key, $expected)) {
// ignore the value, it's not in $expected
continue;
}
if (in_array($key, $required) && empty($value)) {
// required value is missing
$missing[] = $key;
$$key = "";
continue;
}
$$key = $value;
}
}
// validate the user's email
if (!$suspect && !empty($email)) {
$validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($validemail) {
$headers[] = "Reply-To: $validemail";
} else {
$errors['email'] = true;
}
}
# END: require('processmail_05.php');
} // close: if ($_SERVER['REQUEST_METHOD'] == 'POST'))
$tools = true; // Robert's Custom Variable (Do Not Use)
# The header section of the layout.
include("{$_SERVER["DOCUMENT_ROOT"]}/php/includes/header.php");
?>
<main>
<h2><?= $folder_name; ?>
<span><?= $file_name; ?></span>
</h2>
<h2>
HTML Form
<br>Check For Empty Control Values
<br>Check For Valid Email
<br>Check For Suspect Strings
<br>Controls Are Sticky
<br>Debug: Post, Errors, Missing
</h2>
<?php if ($_POST && $suspect) { ?>
<p class="error">Sorry, your mail could not be sent. Please try later.</p>
<?php } elseif ($missing || $errors) { ?>
<p class="error">Please complete the missing item(s) indicated.</p>
<?php } ?>
<form id="feedback" method="post">
<fieldset>
<legend><?php echo $file_name; ?></legend>
<ol>
<li<?php if ($missing && in_array('name', $missing)) echo ' class="error"'; ?>>
<?php if ($missing && in_array('name', $missing)) { ?>
<strong>Please enter your name</strong>
<?php } ?>
<label for="name">Name:</label>
<input name="name" id="name" type="text" class="formbox" <?php if (isset($name)) {
echo 'value="' . htmlentities($name, ENT_COMPAT, 'UTF-8') . '"';
} ?>>
</li>
<li<?php if (($missing && in_array('email', $missing)) || (isset($errors['email']))) echo ' class="error"'; ?>>
<?php if ($missing && in_array('email', $missing)) { ?>
<strong>Please enter your email</strong>
<?php } elseif (isset($errors['email'])) { ?>
<strong>Invalid email address</strong>
<?php } ?>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="formbox" <?php if (isset($email)) {
echo 'value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '"';
} ?>>
</li>
<li<?php if ($missing && in_array('comments', $missing)) echo ' class="error"'; ?>>
<?php if ($missing && in_array('comments', $missing)) { ?>
<strong>Please enter your comments</strong>
<?php } ?>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="60" rows="8"><?php
if (isset($comments)) {
echo htmlentities($comments, ENT_COMPAT, 'UTF-8');
} ?></textarea>
</li>
<li>
<input name="send" id="send" type="submit" value="Send message">
</li>
</ol>
</fieldset>
</form>
<h2>Debug Code</h2>
<pre class="line-numbers">
<code class="language-php">
<?php if ($_POST) {
echo '$_POST: ';
print_r($_POST);
} ?>
<?php if ($errors) {
echo '$errors: ';
print_r($errors);
} ?>
<?php if ($missing) {
echo '$missing: ';
print_r($missing);
} ?>
<?php if (isset($name)) echo '$name: ' . $name; ?>
<?php if (isset($email)) echo '$email: ' . $email; ?>
<?php if (isset($comments)) echo '$comments: ' . $comments; ?>
</code>
</pre>
</main>
<?php
include("{$_SERVER["DOCUMENT_ROOT"]}/php/includes/sidebar.php");
include("{$_SERVER["DOCUMENT_ROOT"]}/php/includes/footer.php");
?>