Ik heb een contactformulier dat ik graag valideer voordat het verstuurd wordt met PHPmailer. Als ik geen gegevens invul of niet de juiste, kom je op een lege contact.php pagina, maar ontvang je geen waarschuwing. Wanneer ik de gevraagde velden in geef wordt de mail netjes verzonden. Waar heb ik een fout waardoor ik geen waarschuwingen ontvang. (De waarschuwingen moeten onder het foutief ingevulde veld verschijnen).
Contact.php ziet er als volgt uit:
Het contact formulier ziet er als volgt uit:
Kan iemand mij zeggen wat ik fout doe?
Dank
Contact.php ziet er als volgt uit:
PHP:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once 'PHPMailerAutoload.php';
$aErrors = array();
if ('POST' == $_SERVER['REQUEST_METHOD']) {
// Er zijn gegevens verstuurd naar deze pagina!
// Een naam bevat letters en spaties (minimaal 3)
if ( !isset($_POST['name']) or !preg_match( '~^[\w ]{3,}$~', $_POST['name'] ) ) {
$aErrors['name'] = 'Please fill in your name';
}
// Een email-adres is wat ingewikkelder
if ( !isset($_POST['email']) or !preg_match( '~^[a-z0-9][a-z0-9_.\-]*@([a-z0-9]+\.)*[a-z0-9][a-z0-9\-]+\.([a-z]{2,6})$~i', $_POST['email'] ) ) {
$aErrors['email'] = 'Please fill in your e-mail address';
}
// Een adres heeft letters, cijfers, spaties (minimaal 5)
if ( !isset($_POST['message']) or !preg_match( '~^[\w\d ]{5,}$~', $_POST['message'] ) ) {
$aErrors['message'] = 'What would you like to share?';
}
if ( count($aErrors) == 0 ) {
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.xs4all.nl'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user'; // SMTP username
$mail->Password = 'pw'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = 'hello@main-site.nl';
$mail->FromName = $_POST['name'];
$mail->addAddress('mains@main-site.nl', 'hello@main-site.nl'); // Add a recipient
// Name is optional
$mail->addReplyTo($_POST['email']);
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact formulier Main-site';
$mail->Body = $_POST['message'];
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if($mail->send()) {
header('Location: localhost:8888/one_page_redirect kopie 3');
die();
} else {
$errors[] ='Sorry, could not send email. Try again later.';
}
}
} else {
$errors[] = 'Something went wrong.';
}
?>
Het contact formulier ziet er als volgt uit:
PHP:
<div class="contact">
<div class="formfield">
<form action="php/contact.php" method="post" class="cmxform">
<div class="fieldset">
<input id="name" class="error_name" name="name" placeholder="What is your name?" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>" />
<?php if (!empty($aErrors['name'])) { ?><div class="error_name"><?php print $aErrors['name'];?></div><?php } ?>
<input id="email" name="email" placeholder="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" />
<?php if (!empty($aErrors['email'])) { ?><div class="error_email"><?php print $aErrors['email'];?></div></div><?php } ?>
<textarea id="message" class="error_message" name="message" placeholder="message" value="<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '' ?>" /></textarea>
<?php if (!empty($aErrors['newt_win_messagev(title, button_text, format, args)'])) { ?><div class="error_message"><?php print $aErrors['message'];?></div></div><?php } ?>
<input name="submit" type="submit" value="Versturen" />
</div>
</form>
</div>
</div>
Kan iemand mij zeggen wat ik fout doe?
Dank
Laatst bewerkt door een moderator: