PDO Prepared Statement - Netmatters Homepage Recreation
PHP
if (!$errors) {
$sql = "INSERT INTO contact_submissions
(name, company, email, telephone, message, marketing_opt_in)
VALUES (:name, :company, :email, :telephone, :message, :marketing)";
$pdo->prepare($sql)->execute([
'name' => $old['name'],
'company' => $old['company'] !== '' ? $old['company'] : null,
'email' => $old['email'],
'telephone' => $old['telephone'],
'message' => $old['message'],
'marketing' => $old['marketing'] ? 1 : 0,
]);
// redirect so refreshing the page can't send the same enquiry twice
header('Location: /');
exit;
}
Stores validated contact form submissions in MySQL using a PDO prepared statement and placeholders to protect against SQL injection.
Optional fields are handled explicitly and the redirect prevents accidental duplicate submissions.
Validating and assigning images to email addresses - JavaScript Array Generator
JavaScript
emailForm.addEventListener('submit', function(event) {
event.preventDefault();
const email = emailInput.value.trim().toLowerCase();
let message = '';
if (!regex.test(email)) {
message = 'enter a valid email address';
} else if (currentSeed === null) {
message = 'generate an image first';
} else if (pairs[email] && pairs[email].includes(currentSeed)) {
message = 'this image is already assigned to that email';
}
emailInput.classList.toggle('input--error', message !== '');
emailInput.classList.toggle('input--success', message === '');
emailError.textContent = message;
if (message !== '') {
return;
}
pairEmailSeed(email, currentSeed);
renderEmails();
});
function pairEmailSeed(email, seed) {
if(!pairs[email]) {
pairs[email] = [];
}
pairs[email].push(seed);
}
Validates user input before pairing generated images with provided email addresses. Image seeds are stored in arrays, with the key being the email address.
Checks prevent invalid input and duplicate pairings. Adds classes to HTML elements based on success state.