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
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:
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
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...
I'd actually do both just incase something goes wrong. The point is that all the data validation should be in the function.
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.
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..
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/
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...
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?
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.
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.
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?
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