-1

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.

Peter28
  • 19
  • 7
  • These are two totally different and non-related issues, so you should have posted one question for each. You don't even show any JavaScript in the question. – Sparky Mar 19 '15 at 19:09
  • It's validating email addresses without the TLD because that is considered a valid address as per the HTML5 spec. If you don't like that, you can write your own rule for `email`. See: http://stackoverflow.com/q/24624404/594235 – Sparky Mar 19 '15 at 19:12
  • Sparky I think that it is your idea that I was disparaging the jQuery validation plugin, but that is the last thing I would do. I am trying to learn some jQuery, php etc. and it is difficult, so the last thing I would do is degenerating some one else his work. The only thing I thought that I was doing something wrong. About the two totally different issues you are completly right, that is my fault. – Peter28 Mar 19 '15 at 19:56
  • I did not think that way at all. I'm simply telling you that the `email` rule is working properly, but I cannot see if you did anything wrong because there is no JavaScript shown. If you want the `email` rule to operate differently, you'll have to use the plugin's `.addMethod()` method to write a custom rule. – Sparky Mar 19 '15 at 20:00
  • Sorry I had made quiet some changes since yesterday and it still didn't work. I hoped that someone could help me out by telling where I went wrong. I have deleted the last one. – Peter28 Mar 20 '15 at 15:15
  • Please edit your question above to better comply with this: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Sparky Mar 20 '15 at 15:58

1 Answers1

0

For server side I use:

if (!filter_var($value, FILTER_VALIDATE_EMAIL))
{
  // Not Valid
}

For client side validation check out: Email validation using jQuery

Community
  • 1
  • 1
danielson317
  • 3,121
  • 3
  • 28
  • 43