If Not with a Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
papabear
Forum Newbie
Posts: 3
Joined: Mon Jan 24, 2011 4:01 pm

If Not with a Function

Post by papabear »

I have a HTML file that allows a client to Approve a Quote. It has all the standard fields in it, Order Number, Name, Phone, E-mail Address, etc. When they click on submit it calls a PHP file to evaluate the information, and if everything is good, it does a $send command to send all the information as an e-mail message to the Admin and then gives the client a Message telling them their Quote has been Approved.

Now most everything works pretty good except a couple of small problem -- actually one major problem.

In the PHP file it evaluates each field to make sure that none of them were left blank. This works fine in that I start with a IF statement that says something like

if ($ordernumber == ' ') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered an Order Number, please go back and try again</b></font></div>";}

so that if the Order Number field is blank they get the message and have to go back to complete the field. If they did complete the field okay then the code would do a ELSEIF to evaluate the next field like

elseif ($businessname == '') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered your Business or Project Name, please go back and try again</b></font></div>";}

Now this works fine and if they complete all the fields it will eventually do a '$send' and change the 'header'

The problem is that in the midst of this I want to validate their e-mail domain to make sure, at a minimum, that the mail domain is validate (not trying to see if the actual mailbox exist).

I split the e-mail address up at the '@' and then have a function called 'checkDNSRR' that checks the validity of the mail domain (this is running on a windows server).

The problem I have is that the way that the code I found for doing the e-mail validation is a standard IF ELSE (e.g. If mail domain is valid echo something and if it is not valid then echo something else) that does not work in my scenario. I need to have this validation happen in the middle of my ELSEIF statements (and not as a ID ELSE) as the final Else, if everything is finally okay (e.g. fields have content and mail domain validated), would then execute the '$send' command.

So I am thinking that the ELSEIF for the email validation really needs to be some sort of ELSEIF NOT, but don't know how to do that. But I may be totally incorrect.

Below is the code that I am trying to work with so maybe someone can tell me how to do this properly. The area where I think I am having problems is line 34.

And if whoever feels really energetic, I am trying to include the date and time the Quote is Approve in the e-mail message that is in the '$send' to the Admin. And lastly, is there an easy way to make validate the format of the phone number field?


<?php

$to = 'sales@breakoutllc.com' ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Quote Approval";
$ordernumber = $_REQUEST['ordernumber'];
$businessname = $_REQUEST['businessname'];
$phone = $_REQUEST['phone'];

$fields = array();
$fields{"ordernumber"} = "ordernumber";
$fields{"businessname"} = "businessname";
$fields{"name"} = "name";
$fields{"phone"} = "phone";
$fields{"email"} = "email";
$fields{"reference"} = "reference";
$fields{"authorizedname"} = "authorizedname";

// take a given email address and split it into the username and domain.
list($userName, $mailDomain) = split("@", $from);

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: noreply@BreakOutLLC.com";
$subject2 = "Quote Approval at BreakOut";
$autoreply = "Thank you for Approving your Quote. Most orders are shipped within 10 working days (approximately 2 weeks depending upon holidays and other scheduled closures)If you have any more questions, please feel free to contact us at 1.888.Ink.On.You or at Sales@BreakOutLLC.com";

if ($ordernumber == '') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered an Order Number, please go back and try again</b></font></div>";}
elseif ($businessname == '') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered your Business or Project Name, please go back and try again</b></font></div>";}
elseif ($name == '') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered your First & Last Name, please go back and try again</b></font></div>";}
elseif ($phone == '') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered your Contact Phone Number, please go back and try again</b></font></div>";}
elseif ($email == '') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered your Contact Phone Number, please go back and try again</b></font></div>";}
elseif (checkDNSRR($mailDomain, "MX") echo "<div align='center'><font color=#bf0c14 size=H5><b>This does NOT appear to be a valid e-mail address, please go back and try again.<br>
If this is a valid e-mail address and we are not able to verify it for some reason,<br>
please contact us at Sales@BreakOutLLC.com</b></font></div>";}
elseif ($from == '') {echo "<div align='center'><font color=#235aca size=H5><b>You have not entered your E-mail Address, please go back and try again</b></font></div>";}
else {

$send = mail($to, $subject, $body, $headers);
//$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{
header('Location: http://www.breakoutcreativecompany.com/ ... gesent.php');
}
else
{echo "<div align='center'><font color=#235aca size=H5><b>We encountered an error Approving your Quote, please contact us at Sales@BreakOutLLC.com</b></font></div>"; }
}

function checkDNSRR($hostName, $recType = '')
{
if(!empty($hostName)) {
if( $recType == '' ) $recType = "MX";
exec("nslookup -type=$recType $hostName", $result);
// check each line to find the one that starts with the host
// name. If it exists then the function succeeded.
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}
}
// otherwise there was no mail handler for the domain
return false;
}
return false;
}

?>
Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

Re: If Not with a Function

Post by Peec »

Please use code tags.

However, i guess this will be working ? I cleaned up that code a bit and made use of filter_var, requires PHP 5.2 +.

Code: Select all



<?php

$to = 'sales@breakoutllc.com' ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Quote Approval";
$ordernumber = $_REQUEST['ordernumber'];
$businessname = $_REQUEST['businessname'];
$phone = $_REQUEST['phone'];

$fields = array('ordernumber','businessname','name','phone','email','reference','authorizedname');




$body = "We have received the following information:\n\n"; 
foreach($fields as $field){ 
	$body .= $field.': '.$_REQUEST[$a]."\n"; 
}
$headers2 = "From: noreply@BreakOutLLC.com";
$subject2 = "Quote Approval at BreakOut";
$autoreply = "Thank you for Approving your Quote. Most orders are shipped within 10 working days (approximately 2 weeks depending upon holidays and other scheduled closures)If you have any more questions, please feel free to contact us at 1.888.Ink.On.You or at Sales@BreakOutLLC.com";

if ($ordernumber == '') {
	echo errorMsg('You have not entered an Order Number, please go back and try again');
}elseif ($businessname == '') {
	echo errorMsg('You have not entered your Business or Project Name, please go back and try again');
}elseif ($name == '') {
	echo errorMsg('You have not entered your First & Last Name, please go back and try again');
}elseif ($phone == '') {
	echo errorMsg('You have not entered your Contact Phone Number, please go back and try again');
}elseif ($email == '') {
	echo errorMsg('You have not entered your Contact Phone Number, please go back and try again');
}elseif ($from == '') {
	echo errorMsg('You have not entered your E-mail Address, please go back and try again');
}elseif (!filter_var($from, FILTER_VALIDATE_EMAIL)){
	echo errorMsg('This does NOT appear to be a valid e-mail address, please go back and try again.<br />
	If this is a valid e-mail address and we are not able to verify it for some reason,<br />
	please contact us at Sales@BreakOutLLC.com');
}else {

	$send = mail($to, $subject, $body, $headers);
	//$send2 = mail($from, $subject2, $autoreply, $headers2);
	if($send){
		header('Location: http://www.breakoutcreativecompany.com/ ... esent.php');
		die();
	}else{
		echo errorMsg('We encountered an error Approving your Quote, please contact us at Sales@BreakOutLLC.com'); 
	}
}


function errorMsg($msg){
	return '<div align="center"><font color=#235aca size=H5><b>'.$msg.'</b></font></div>';
}

?>
papabear
Forum Newbie
Posts: 3
Joined: Mon Jan 24, 2011 4:01 pm

Re: If Not with a Function

Post by papabear »

Thanks, that really helped. Solved some of my problems. The only problem is that now it checks to make sure that there is a properly formatted e-mail address but, without the 'checkDNSRR' function in the original code, it does not check for a valid e-mail domain. It will pretty much accept any e-mail address as long as it is formatted as something@something.anything. For instance if I entered papabear@san.gen the 'checkDNSRR' function would determine that 'san.gen' was NOT a valid e-mail domain, however if the address was papabear@san.com it would determine that it WAS a valid e-mail domain. So I lost that capability in the rewrite.

Also I was wondering if there was:

1. A way to check phone number format and
2. A way to capture the date & time as one of the fields that is picked up and included in the $body to be e-mailed as part of the $send
Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

Re: If Not with a Function

Post by Peec »

US phone number validation
and date in body.

Code: Select all

<?php

$to = 'sales@breakoutllc.com' ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Quote Approval";
$ordernumber = $_REQUEST['ordernumber'];
$businessname = $_REQUEST['businessname'];
$phone = $_REQUEST['phone'];

$fields = array('ordernumber','businessname','name','phone','email','reference','authorizedname');




$body = "We have received the following information:\n\n"; 
foreach($fields as $field){ 
	$body .= $field.': '.$_REQUEST[$a]."\n"; 
}
// Date add
$body .= "We got this message at: ".date('r', time());

$headers2 = "From: noreply@BreakOutLLC.com";
$subject2 = "Quote Approval at BreakOut";
$autoreply = "Thank you for Approving your Quote. Most orders are shipped within 10 working days (approximately 2 weeks depending upon holidays and other scheduled closures)If you have any more questions, please feel free to contact us at 1.888.Ink.On.You or at Sales@BreakOutLLC.com";
$from_split = explode('@', $from);

if ($ordernumber == '') {
	echo errorMsg('You have not entered an Order Number, please go back and try again');
}elseif ($businessname == '') {
	echo errorMsg('You have not entered your Business or Project Name, please go back and try again');
}elseif ($name == '') {
	echo errorMsg('You have not entered your First & Last Name, please go back and try again');
}elseif ($phone == '') {
	echo errorMsg('You have not entered your Contact Phone Number, please go back and try again');
}elseif ($email == '') {
	echo errorMsg('You have not entered your Contact Phone Number, please go back and try again');
}elseif ($from == '') {
	echo errorMsg('You have not entered your E-mail Address, please go back and try again');
}elseif (!filter_var($from, FILTER_VALIDATE_EMAIL) || !checkDNSRR($from_split[1])){
	echo errorMsg('This does NOT appear to be a valid e-mail address, please go back and try again.<br />
	If this is a valid e-mail address and we are not able to verify it for some reason,<br />
	please contact us at Sales@BreakOutLLC.com');

// Matches US Phone number.
}elseif(!preg_match('/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/', $phone)){
	echo errorMsg('Your phone number seems to be incorrect. US Number is allowed.');
}else {

	$send = mail($to, $subject, $body, $headers);
	//$send2 = mail($from, $subject2, $autoreply, $headers2);
	if($send){
		header('Location: http://www.breakoutcreativecompany.com/ ... esent.php');
		die();
	}else{
		echo errorMsg('We encountered an error Approving your Quote, please contact us at Sales@BreakOutLLC.com'); 
	}
}




function checkDNSRR($hostName, $recType = 'MX'){
	if(!empty($hostName)) {
		exec("nslookup -type=$recType $hostName", $result);
		// check each line to find the one that starts with the host
		// name. If it exists then the function succeeded.
		foreach ($result as $line) {
			if(eregi("^$hostName",$line)) {
				return true;
			}
		}
	}
	return false;
}

function errorMsg($msg){
	return '<div align="center"><font color=#235aca size=H5><b>'.$msg.'</b></font></div>';
}

?>
papabear
Forum Newbie
Posts: 3
Joined: Mon Jan 24, 2011 4:01 pm

Re: If Not with a Function

Post by papabear »

Thanks so very much. I will give it a shot today and let you know how it goes.
Post Reply