Mailer Problem

Discussion in 'PHP' started by Jeremy Benson, May 15, 2014.

  1. #1
    Hey,

    I picked PHP back up, but I've regressed a bit. I started working on this mailer, but there's a problem in the code. When the send message button is clicked I get two messages displayed instead of one. The browser also displays some of the php code. There's a message for mail sent properly, and one for error in form (empty field). If anyone could help that would be awesome.

    Processing code
    
    <?
    $senderName = $_POST['name'];
    $senderLastName = $_POST['lastName'];
    $senderEmailAddress = $_POST['emailAddress'];
    $senderSubject = $_POST['subject'];
    $senderDepartment = $_POST['departments'];
    $senderMessage = $_POST['message'];
    $errorCount = 0;
    $mailTo = 0;
    $emailHeader = 0;
    
       if(!isset($_POST['name']))
         {
           $errorCount = $errorCount + 1;
         }
         
       if(!isset($_POST['lastName']))
       }
         $errorCount = $errorCount + 1;
    
       }
       
       if(!isset($_POST['emailAddress']))
       {
         $errorCount = $errorCount + 1;
    
       }
       
       if(!isset($_POST['subject']))
       {
         $errorCount = $errorCount + 1;
    
       }
       
       if(!isset($_POST['message']))
       {
         $errorCount = $errorCount + 1;
       }
       
       //Set the email to address.
       
       if($_POST['departments'] == "Info")
       {
       
         $mailTo = "emailAddress1";
       
       }
       
       if($_POST['departments'] == "Advertising")
       {
       
         $mailTo = "emailAddress2";
       
       }
       
       // Send the message!
         
       if($errorCount == 0)
       {   
         //Set the header
        $emailHeader = "From: " . $senderName . $senderLastName . " " . $senderEmail;
       
           //Send the message
        //   mail($mailTo, $senderSubject, $senderMessage, $emailHeader);
       }
    
    ?>
    
    PHP:
    Display code
    
    <?
         
       if($errorCount == 0)
       {   
         
         echo "<h3>Thank you!</h3>";
         
         echo "<p>Your message has been sent. Please wait patiently for a reply. If there's a high volume of messages its possible that some replies will take longer than others.</p>";
       
       }elseif($errorCount > 0)
       {
         
         echo "<h3>There was an error!</h3>";
         
         echo "<p>Sorry you forgot to fill out one of the fields. Please go back and be sure all fields are filled in.</p>";
    
       }
    
    ?>
    
    PHP:
    Thanks :)
     
    Jeremy Benson, May 15, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Here's how I'd tackle that job. I'm not a fan of global counters and you really need to groom that $_POST data before you send it anywhere
    <?php
    class mailHandler {
    
        public $errorCount = 0;
    
        private function getPostVariable($key) {
            $output = '';
            if (!isset($_POST[$key]))
                $this->errorCount++;
            else
                $output = $_POST[$key];
            //check for security issues etc at this point
            //never use content that hasn't been screened
            return $output;
        }
    
        public function mailHandler() {
            $senderName = $this->getPostVariable('name');
            $senderLastName = $this->getPostVariable('lastName');
            $senderEmail = $this->getPostVariable('emailAddress');
            $senderSubject = $this->getPostVariable('subject');
            $senderDepartment = $this->getPostVariable('departments');
            $senderMessage = $this->getPostVariable('message');
    
            switch ($senderDepartment):
                case 'Info':
                    $mailTo = "emailAddress1";
                    break;
                case 'Advertising':
                    $mailTo = "emailAddress2";
                    break;
                default:
                    $this->errorCount++;
            endswitch;
    
            if ($this->errorCount == 0) {
    //Set the header
                $emailHeader = "From: " . $senderName . $senderLastName . " " . $senderEmail;
    
    //Send the message
    //   mail($mailTo, $senderSubject, $senderMessage, $emailHeader);
            }
        }
    
    }
    
    $mh = new mailHandler();
    $errorCount = $mh->errorCount;
    
    if ($errorCount == 0) {
    
        echo "<h3>Thank you!</h3>";
        echo "<p>Your message has been sent. Please wait patiently for a reply. If there's a high volume of messages its possible that some replies will take longer than others.</p>";
    } else { //you don't need an elseif
        echo "<h3>There was an error!</h3>";
        echo "<p>Sorry you forgot to fill out one of the fields. Please go back and be sure all fields are filled in.</p>";
    }
    PHP:
     
    sarahk, May 15, 2014 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Hey, thanks for in-depth OOP version :)... I usually avoid that route for simple static sites like I make, but since you took the time I'll save the code and go through it :)
     
    Jeremy Benson, May 16, 2014 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Hey, I played with the code here and it looks like it'll work, but I only get a thank you message... I tested errorCount and it's always 0...
    I guess the if statement never fires when there's an empty field...

    
     if (!isset($_POST[$key]))
           {
          $this->errorCount++;
           }
    
    
    PHP:
    I tried changing the condition to..

     if ($_POST[$key] == " ")
    PHP:
    but that didn't work either...
     
    Jeremy Benson, May 17, 2014 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Try changing
    if (!isset($_POST[$key]))
    to
    if (empty($_POST[$key]))

    and see if that works
     
    PoPSiCLe, May 17, 2014 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #6
    I'd actually do both just incase something goes wrong. The point is that all the data validation should be in the function.
     
    sarahk, May 17, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    No reason to do both isset() and empty() - empty() does most of what isset does, except throw a false if the value of the variable is explicity NULL - and I don't really think that's a very likely scenario in the case of a mailer.
     
    PoPSiCLe, May 17, 2014 IP
  8. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #8
    cool, I'll try that when I get home. Thanks for the help guys. Appreciate it as always.
     
    Jeremy Benson, May 19, 2014 IP
  9. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #9
    Hey, while I'm here... I was wondering. Could you tell me the difference between a set, unset, and null variable. I notice functions isset, empty, and is_null..
     
    Jeremy Benson, May 19, 2014 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    isset() == checks if the variable is actually set
    empty() == checks if the variable is empty (implies/assumes it's set)
    is_null() == checks if the variable is exactly NULL

    However, especially empty() can be somewhat confusing for new coders. I recommend the following link: http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/
     
    PoPSiCLe, May 19, 2014 IP
  11. Jigney

    Jigney Active Member

    Messages:
    168
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    93
    #11
    You can use

    if(!isset($_POST['name'] && $_POST[‘name’] != ‘’) for the validation and to display a message just use
    if($errorCount==0){}
    else{}

    Because if you don’t have 3 possibilities than why you are using else if...
     
    Jigney, May 20, 2014 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    Not entirely true.

    isset() checks if a variable has been declared, and if it's null. $foo = null; will fail the isset() check.

    empty() checks if a variable is empty, but does not assume it's set. (This is probably what you want for form validation). It's a language construct like isset(), and won't throw error notices on unset variables. So using isset() and empty() concurrently is pointless.

    is_null() is one of these functions you rarely need. If ever, that is. Why would you want to call a function when you can do $foo === null, which is a lot faster?
     
    Last edited: May 20, 2014
    nico_swd, May 20, 2014 IP
  13. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #13
    Uhm... empty() checks if a variable is empty/null - which means it implicitly assumes the variable is set (or, as you said, doesn't throw an error if it isn't set) - in my book, this assumes the variable is set. Of course, this might bite you later, if you then just use the variable regardless of the return of the empty()-check, but then you're just stupid.
     
    PoPSiCLe, May 20, 2014 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    Well, I guess it depends on how you look at it. In my book, it does not assume a variable is set, because it doesn't throw any notices if it's not. And according to the PHP manual:
    But like I said, it probably depends on how you look at it.
     
    nico_swd, May 21, 2014 IP
  15. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #15
    Yeah, I guess - I think we're agreeing, just looking at it from different angles.
     
    PoPSiCLe, May 21, 2014 IP
  16. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #16
    Thanks for the resources. Learning a bit here, but something I never really looked into. What's the difference between set and unset. does set mean it's been given a value?

    $foo;
    $bar = 12;

    $foo is unset, $bar is set?
     
    Jeremy Benson, May 22, 2014 IP
  17. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #17
    Well... not quite.
    $foo; // unset (technically, this is a variable called, not assigned, since the simple declaration of a value without any assigned values/non-values is a call in PHP
    $foo = ''; //set
    $foo = 'somevalue'; //set
    $foo = NULL; //unset
    $foo = false; //set
    $foo = 0; //set
     
    PoPSiCLe, May 22, 2014 IP