Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    On Page Form Validation

    I have this installed this code at this link The code is based on the tutorial by boxmodeljunkie.com

    Why will it not validate the fields?


    This is the contact.php


    PHP Code:
    <?php
    // we must never forget to start the session
    session_start();
    ?>

    <?php
    define
    ("EMAIL""yyyyyyyy@yyyy.yyy");

    $messageErr "";
    $message_text "";
    $errors "";
    $nameErr "";
    $emailErr "";
    $messageErr "";
    $name "";
    $email "";
    $message "";
     
    if(isset(
    $_POST['submit'])) {
     
      include(
    'validate.class.php');
     
      
    //assign post data to variables
      
    $name trim($_POST['name']);
        
    $email trim($_POST['email']);
        
    $message trim($_POST['message']);
     
      
    //start validating our form
      
    $v = new validate();
      
    $v->validateStr($name"name"375);
      
    $v->validateEmail($email"email");
      
    $v->validateStr($message"message"51000); 
     
      if(!
    $v->hasErrors()) {
            
    $header "From: $email\n" "Reply-To: $email\n";
            
    $subject "Contact Form Subject";
            
    $email_to EMAIL;
     
            
    $emailMessage "Name: " $name "\n";
            
    $emailMessage .= "Email: " $email "\n\n";
            
    $emailMessage .= $message;
     
        
    //use php's mail function to send the email
            
    @mail($email_to$subject ,$emailMessage ,$header ); 
     
        
    //grab the current url, append ?sent=yes to it and then redirect to that url
            
    $url "http". ((!empty($_SERVER['HTTPS'])) ? "s" "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
            
    header('Location: '.$url."?sent=yes");
     
        } else {
        
    //set the number of errors message
        
    $message_text $v->errorNumMessage();      
     
        
    //store the errors list in a variable
        
    $errors $v->displayErrors();
     
        
    //get the individual error messages
        
    $nameErr $v->getError("name");
        
    $emailErr $v->getError("email");
        
    $messageErr $v->getError("message");
      }
    //end error check
    }// end isset
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="robots" content="noindex, nofollow"/>
    <title>| PHP Form Code | </title>
    <style type="text/css">
    body {
      margin:0;
      padding:0;
      font-family:Arial, Helvetica, sans-serif;
      font-size:12px;
      background-color:#101a25;
      color:#fff;
    }
    #contact_form_wrap {
      margin:0 auto;
      margin-top:50px;
      padding:10px;
      width:350px;
    }
    .message {
      font-weight:bold;
    }
    .errors {
      color:#af1010;
    }
    label {
        font-weight:bold;
    }
    .textfield {
        padding:5px 0 0 3px;
        width:297px;
        height:20px;
        border: 1px solid #e9e9e9;
        background-color:#303942;
        color:#fff;
    }
    .textarea {
        padding:3px;
        width:294px;
        height:144px;
        border: 1px solid #e9e9e9;
        background-color:#303942;
        font-family:Arial, Helvetica, sans-serif;
        font-size:12px;
        color:#fff;
    }
    .button {
        padding:3px 0 5px 0;
        width:75px;
        height:25px;
        border: none;
        background-color:#303942;
        font-weight:bold;
        cursor:pointer;
        color: #fff;
        font-family:Arial, Helvetica, sans-serif;
    }
    .button:hover {
        background-color:#f1f1f1;
        color: #333;
    }
    </style>
    </head>
    <body>
      <div id="contact_form_wrap">
        <span class="message"><?php echo $message_text?></span>
        <?php echo $errors?>
        <?php if(isset($_GET['sent'])): ?><h2>Your message has been sent</h2><?php endif; ?>
        <form id="contact_form" method="post" action=".">
          <p><label>Name:<br />
          <input type="text" name="name" class="textfield" value="<?php echo htmlentities($name); ?>" />
          </label><br /><span class="errors"><?php echo $nameErr?></span></p>
     
          <p><label>Email: <br />
          <input type="text" name="email" class="textfield" value="<?php echo htmlentities($email); ?>" />
          </label><br /><span class="errors"><?php echo $emailErr ?></span></p>         
     
          <p><label>Message: <br />
          <textarea name="message" class="textarea" cols="45" rows="5"><?php echo htmlentities($message); ?></textarea>
          </label><br /><span class="errors"><?php echo $messageErr ?></span></p>
     
          <p><input type="submit" name="submit" class="button" value="Submit" /></p>
        </form>
      </div>
     
    </body>
    </html>

    This is validate.class.php


    PHP Code:
    <?php
    // we must never forget to start the session
    session_start();
    ?>
    <?php
    class validate {
     
      
    // ---------------------------------------------------------------------------
      //  paramaters
      // ---------------------------------------------------------------------------
     
      /**
      * Array to hold the errors
      *
      * @access public
      * @var array
      */
      
    public $errors = array();
     
      
    // ---------------------------------------------------------------------------
      //  validation methods
      // ---------------------------------------------------------------------------
     
      /**
      * Validates a string
      *
      * @access public
      * @param $postVal - the value of the $_POST request
      * @param $postName - the name of the form element being validated
      * @param $min - minimum string length
      * @param $max - maximum string length
      * @return void
      */
      
    public function validateStr($postVal$postName$min 5$max 500) {
        if(
    strlen($postVal) < intval($min)) {
          
    $this->setError($postNameucfirst($postName)." must be at least {$min} characters long.");
        } else if(
    strlen($postVal) > intval($max)) {
          
    $this->setError($postNameucfirst($postName)." must be less than {$max} characters long.");
        }
      }
    // end validateStr
     
      /**
      * Validates an email address
      *
      * @access public
      * @param $emailVal - the value of the $_POST request
      * @param $emailName - the name of the email form element being validated
      * @return void
      */
      
    public function validateEmail($emailVal$emailName) {
        if(
    strlen($emailVal) <= 0) {
          
    $this->setError($emailName"Email Address Required");
        } else if (!
    preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/'$emailVal)) {
          
    $this->setError($emailName"Valid Email Address Required");
            }
      }
    // end validateEmail
     
      // ---------------------------------------------------------------------------
      //  error handling methods
      // ---------------------------------------------------------------------------
     
      /**
      * sets an error message for a form element
      *
      * @access private
      * @param string $element - name of the form element
      * @param string $message - error message to be displayed
      * @return void
      */
      
    private function setError($element$message) {
        
    $this->errors[$element] = $message;
      }
    // end logError
     
      /**
      * returns the error of a single form element
      *
      * @access public
      * @param string $elementName - name of the form element
      * @return string
      */
      
    public function getError($elementName) {
        if(
    $this->errors[$elementName]) {
          return 
    $this->errors[$elementName];
        } else {
          return 
    false;
        }
      }
    // end getError
     
      /**
      * displays the errors as an html un-ordered list
      *
      * @access public
      * @return string: A html list of the forms errors
      */
      
    public function displayErrors() {
        
    $errorsList "<ul class=\"errors\">\n";
        foreach(
    $this->errors as $value) {
          
    $errorsList .= "<li>"$value "</li>\n";
        }
        
    $errorsList .= "</ul>\n";
        return 
    $errorsList;
      }
    // end displayErrors
     
      /**
      * returns whether the form has errors
      *
      * @access public
      * @return boolean
      */
      
    public function hasErrors() {
        if(
    count($this->errors) > 0) {
          return 
    true;
        } else {
          return 
    false;
        }
      }
    // end hasErrors
     
      /**
      * returns a string stating how many errors there were
      *
      * @access public
      * @return void
      */
      
    public function errorNumMessage() {
        if(
    count($this->errors) > 1) {
                
    $message "There were " count($this->errors) . " errors sending your message!\n";
            } else {
                
    $message "There was an error sending your message!\n";
            }
        return 
    $message;
      }
    // end hasErrors
     
    }// end class

  • #2
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Getting no error messages in the error log. When filling out the form fields incorrectly errors should present underneath the relevant fields but nothing like that is happening! How would I know that the validate.class.php is communicating with the contact.php ?

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by iBall View Post
    You can put an echo statement in one of the class' methods to confirm it is being reached.

    Code:
     public function validateStr($postVal, $postName, $min = 5, $max = 500) { 
    
        echo 'Got inside validateStr'; die();
    I installed this echo statement and got no response - so it can now be taken that the two files are not talking to each other or as you suggest.... being reached!

    How do we get them to reach out to each other if the include('validate.class.php'); is not functioning as expected?

  • #4
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Both files in the same directory.
    Have now changed validate_class.php to validateClass.php.
    Sorry about the Shudder but yes the validateClass looks better.
    When I run in browser or wamp still no response or errors?

  • #5
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Bare bones code now installed in the validateClass.php file!

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    if your validation class was not getting loaded you should be getting a fatal error when trying to call $v = new validate(); so something else is wrong....

    at the top of contact.php add
    PHP Code:
    ERROR_REPORTING(E_ALL); 
    you may then see some other error that might give you a clue.
    changing file names wont help a jot
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    webiter (06-17-2012)

  • #7
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Have all this at the top of contact.php and no errors being presented!

    PHP Code:
    <?php
    ini_set
    ('display_errors'1);
    ini_set('log_errors'1);
    ini_set('error_log'dirname(__FILE__) . '/error_log.txt');
    ERROR_REPORTING(E_ALL);
    ?>
    When in wamp I get a quick flash of Not Found The requested URL was not found on this server.

    On the web See Here

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    <form id="contact_form" method="post" action=".">
    change to
    <form id="contact_form" method="post" action="?">

    or the full url of where the form is getting submitted
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    webiter (06-18-2012)

  • #9
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Great one firepages your ? worked a treat. Thanks a lot

    Might I advance a question... why would the error process
    not give any indication as to the problem that was occurring?

    It is a nice form code and validation that anybody could use.

    Also, thanks to everybody else for their help.

  • #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    Quote Originally Posted by webiter View Post
    Might I advance a question... why would the error process
    not give any indication as to the problem that was occurring?
    your form was not actually being submitted (at least not to the right place) so validate never even got a look in, at least thats my assumption. anyway glad its sorted.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #11
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Maybe this should be a new topic/thread

    When I change the action on the line from this

    PHP Code:
    <form id="contact_form" method="post" action="?"
    to this, introducing a formHandler file

    PHP Code:
    <form id="contact_form" method="post" action="formHandler.php"
    and move all of the class validate code { } to the formHandler.php file, the files will not talk to each other.

    Is it a simple task to get the contact.php to be actioned by the formHandler.php ?

    Thanks in advance for any opinion on this item.

  • #12
    New to the CF scene
    Join Date
    Jun 2012
    Location
    Rep of Ireland
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Form at this link tests out fine on Wamp and presents the
    message "Your message has been sent" as required after submit.
    However on the web it does not show the message but goes to
    an external web page.

    Why is it being redirected on the web?

    PHP Code:
    <?php if(isset($_GET['sent'])): ?><h2>Your message has been sent</h2><?php endif; ?>
          <form id="contact_form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"/>
    Thanks in advance.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •