www.webdeveloper.com
Results 1 to 3 of 3

Thread: Mis-directed contact form on send

  1. #1
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64

    Mis-directed contact form on send

    The form action="<?php echo $_SERVER['PHP_SELF'];?>

    yet the form when sent presents a Oops page! Page not found in lieu of the "Your message has been sent"

    The form handler is as follows:

    PHP Code:
    <?php
    define
    ("EMAIL""xxxxxx@gmail.com");

    $messageErr "";
    $message_text "";
    $errors "";

    $nameErr "";
    $emailErr "";
    $phoneErr "";
    $messageErr "";
    $codeErr "";

    $name "";
    $email "";
    $phone "";
    $message "";
    $code "";

    if(isset(
    $_POST['submit'])) {
     
      include(
    'validateClass.php');
     
      
    //assign post data to variables
        
    $name    trim($_POST['name']);
        
    $email   trim($_POST['email']);
        
    $phone   trim($_POST['phone']);
        
    $message trim($_POST['message']);
        
    $code    trim($_POST['code']);
     
      
    //start validating our form
      
    $v = new validate();
      
    $v->validateStr($name"name"375);
      
    $v->validateEmail($email"email");
      
    $v->validatePhone($phone"phone");
      
    $v->validateStr($message"message"10500); 
      
    $v->validateCode($code"code");
     
      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";
            
    $emailMessage .= "Phone:   " $phone "\n\n";
            
    $emailMessage .= "Message: " $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");
        
    $phoneErr   $v->getError("phone");
        
    $messageErr $v->getError("message");
        
    $codeErr    $v->getError("code");
      } 
    //end of the error check
    }
    // end isset
    ?>
    and the "Your message has been sent" is built in at the top of the form is as follows:

    PHP Code:
    <?php if(isset($_POST['sent'])): ?><h2>Your message has been sent</h2><?php endif; ?>
    Why is the process sending to the Oops page ?

  2. #2
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    437
    1) Is the mail getting sent ?

    2) What is in validateClass.php ?
    Chris, Senior Developer, Chrisranjana.com
    Php Programmers

  3. #3
    Join Date
    Jul 2011
    Location
    Irish Republic
    Posts
    64
    Yes, the mail is being sent and being received in the mailbox.

    The 'validateClass.php is as follows:

    PHP Code:

    <?php

    class validate {

        public 
    $errors = array();

        
    /* Validate message string */
        
    public function validateStr($postVal$postName$min 10$max 500) {    
            if (
    strlen($postVal) < intval($min)) {
                
    $this->setError($postNameucfirst($postName) . " at least {$min} characters long.");
            } else if (
    strlen($postVal) > intval($max)) {
                
    $this->setError($postNameucfirst($postName) . " less than {$max} characters long.");
            }
        }
        
    /* Validate email address */
        
    public function validateEmail($emailVal$emailName) {
            if (
    strlen($emailVal) <= 0) {
                
    $this->setError($emailName"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 Address Required");
            }
        }
        
    /* Validate phone no to allow numbers only */    
        
    public function validatePhone($phoneVal$phoneName) {
            if (
    strlen($phoneVal) <= 0) {
                
    $this->setError($phoneName"Number Required"); 
            } else if (
    preg_match('/[^0-9]/'$phoneVal)) { 
                
    $this->setError($phoneName"Valid Number Required");
            }
        }
        
    /* Validate captcha entry*/    
            
    public function validateCode($codeVal$codeName) {
            if (
    strlen($codeVal) <= 0) {
                
    $this->setError($codeName"Enter Validate Code");            
            } else if(
    md5($codeVal) != $_SESSION['image_random_value']) {    
                
    $this->setError($codeName"Valid Code Required");
            }
        }
        
    /* sets an error message for a form element*/    
        
    private function setError($element$message) {
            
    $this->errors[$element] = $message;
        }
        
    /* returns the error of a single form element*/
        
    public function getError($elementName) {
            if (
    $this->errors[$elementName]) {
                return 
    $this->errors[$elementName];
            } else {
                return 
    false;
            }
        }
        
    /* displays the errors as an html un-ordered list*/
        
    public function displayErrors() {
            
    $errorsList "<ul class=\"errors\">\n";
            foreach (
    $this->errors as $value) {
                
    $errorsList .= "<li>" $value "</li>\n";
            }
            
    $errorsList .= "</ul>\n";
            return 
    $errorsList;
        }
        
    /* returns whether the form has errors*/
        
    public function hasErrors() {
            if (
    count($this->errors) > 0) {
                return 
    true;
            } else {
                return 
    false;
            }
        }
        
    /* returns a string stating how many errors there were*/
        
    public function errorNumMessage() {
            if (
    count($this->errors) > 1) {
                
    $message "There were " count($this->errors) . " errors sending your message!\n";
            } else {
                
    $message "Please correct the error to send your message!\n";
            }
            return 
    $message;
        }
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles