I am trying to validate a contactform serversite with php and client site with the jQuery validation plugin. First when I use jQuery validation plugin after entering a email adress and I entered @ and 1 character after the @ the jQuery validation plugin will pass the email adress even without .com, .org, etc. At there demo site you can see what I mean: [http://jqueryvalidation.org/files/demo][1]. Second I have tried to validate the server site with PHP but that doesn't work and I can't find out where it is going wrong. The validation file looks like:
<?php
// We gaan de errors in een array bijhouden
$aErrors = array();
$aErrors ['name'] = '';
$aErrors ['email'] = '';
$aErrors ['message'] = '';
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 ]{2,}$~', $_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 ) {
// We hebben alle gegevens
// Gegevens verwerken!
// Volgende pagina aub
header('Location: http://www.phpfreakz.nl/someotherpage.php');
exit();
}
}
?>
The validation.php is required once at the top of the index.php, like:
<?php require_once 'php/validation.php'; ?>
<!doctype html>
<html lang="en" class="no-js">
<head>
<title>Alternate Fixed And Scroll Backgrounds</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
The contact form it self looks like:
<div class="contact">
<div class="formfield">
<form method="post" class="cmxform" id="commentForm" action="php/contact.php">
<div class="fieldset">
<input id="name" name="name" class="error_name" type:"text" placeholder="What is your name?" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>" />
<div class="error_name"><?php print $aErrors['name'];?></div>
<input id="email" name="email" class="error_email" type="email" placeholder="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" />
<div class="error_email"><?php print $aErrors['email'];?></div>
<textarea id="message" name="message" class="error_message" placeholder="message" value="<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '' ?>" /></textarea>
<div class="error_message"><?php print $aErrors['message'];?></div>
<input name="submit" type="submit" value="Versturen" />
</div>
</form>
</div>
</div>
What am I doing wrong with the PHP validation that it doesn't work. When I remove:
<?php require_once 'php/validation.php'; ?>
I receive an error, so it knows it is there but doesn't do anything with it.
This is how I have changed the contact form. Validation now is placed in front of the PHPmailer. When the contact form is entered correctly the mail is send as it should. But when the form isn't entered correctly and I click submit there will be no error message displayed and I will be transfered to a blanc contact.php page.
So is how contact.php looks:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once 'PHPMailerAutoload.php';
$aErrors = array();
$aErrors ['name'] = '';
$aErrors ['email'] = '';
$aErrors ['message'] = '';
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 = 'xxxxxxxxxxxxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = 'xxxxxxxxx';
$mail->FromName = $_POST['name'];
$mail->addAddress('x', 'x'); // 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';
$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.';
}
?>
The contact form looks like:
<div class="contact">
<div class="formfield">
<form action="php/contact.php" method="post" class="cmxform">
<?php
if ( isset($aErrors) and count($aErrors) > 0 ) {
foreach ( $aErrors as $error ) {
print '<div>' . $error . '</div>';
}
}
?>
<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><?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><?php } ?>
<input name="submit" type="submit" value="Versturen" />
</div>
</form>
</div>
</div>
Thanx in advance.