Page 1 of 1

Mail form only sometimes works

Posted: Sun Jan 23, 2011 12:20 pm
by Toubab
Hello, this is my first post here and I'm still a bit of a noob with PHP...so go easy! :)

I searched as best I could for a solution to this but I haven't been too successful. I have a form and I followed some tutorials I found on other sites to validate the user supplied variables then email them. I wish I had found this site first because I think the tutorials I followed were out of date, but that's life, so I'm trying to see if I can at least get this thing to work without having to completely redo it.

The problem, as best as I can tell, is that it sometimes goes through and sometimes doesn't. I haven't been able to find a pattern either. That's what's got me so confused. If my code works, shouldn't it work every time?

I know that the "formvalidator.php" file I'm using contains some depreciated functions but it was working so I thought, if it ain't broke... Could that be causing my problems? I could post that here too, but it's pretty long so I don't know if I should just put the whole thing up or what.

You can see the form online at http://www.kreweofpetronius.org/apply. And here's the code behind it:

Code: Select all

<?PHP
require_once "formvalidator.php";
$show_form=true;

if(isset($_POST['submit']))
{
	// Form validation:
    $validator = new FormValidator();
    $validator->addValidation("name","req","Please fill in your name");
	$validator->addValidation("addressLine1","req","Please fill in your address");
	$validator->addValidation("city","req","Please fill in city");
	$validator->addValidation("state","dontselect=Select One","Please select a state");
	$validator->addValidation("zipCode","req","Please enter your zip code");
	$validator->addValidation("zipCode","num","Zip code should only contain numbers");
	$validator->addValidation("zipCode","minlen=5","Please enter a valid, 5 digit zip code");
	$validator->addValidation("phoneNum","req","Please enter a phone number");
    $validator->addValidation("email","email","The input for Email should be a valid email address (ex:'name@emailprovider.com')");
    $validator->addValidation("email","req","Please fill in your email");
	$validator->addValidation("beMemberBecause","req","Please fill in why you would like to become a member");
	$validator->addValidation("previousMember","selone","Please indicate if you have previously been a member of the Krewe of Petronius or another Gay Mardi Gras Organization");
	$validator->addValidation("agreeToTerms","shouldselchk=yes","You must check the box to indicate you agree to the terms of this application.");
	//User supplied variables:
	$applicantName = $_POST["name"];
	$addressLine1 = $_POST["addressLine1"];
	$addressLine2 = $_POST["addressLine2"];
	$city = $_POST["city"];
	$state = $_POST["state"];
	$zipCode = $_POST["zipCode"];
	$homePhone = $_POST["phoneNum"];
	$cellPhone = $_POST["phoneNumCell"];
	$applicantEmail = $_POST["email"];
	$beMemberBecause = $_POST["beMemberBecause"];
	$previousMember = $_POST["previousMember"];
	$whyLeftPrevious = $_POST["whyLeftPrevious"];
	$specialSkills = $_POST["specialSkills"];
	$agreeToTerms = $_POST["agreeToTerms"];
	$sponser1 = $_POST["sponser1"];
	$sponser2 = $_POST["sponser2"];
	$sponser3 = $_POST["sponser3"];
	$sponser4 = $_POST["sponser4"];
	$sponser5 = $_POST["sponser5"];
	
	
    if($validator->ValidateForm())
    {
        // successful submit actions
		//$recipient = "applications@kreweofpetronius.org"; //email for the organization
		$recipient = "xxxx@gmail.com"; //my email for testing
		$emailText = 
		"$applicantName has submitted an online application.  Here is their information:\n\n".
		"Name: $applicantName \n".
		"Address: $addressLine1 \n".
		"$addressLine2 \n".
		"$city, $state $zipCode \n".
		"Phone: $homePhone \n".
		"Cell Phone: $cellPhone \n".
		"Email: $applicantEmail \n".
		"I would like to become a member of the Krewe of Petronius because: $beMemberBecause \n".
		"Have you ever been a member of the Krewe of Petronius or any other gay Mardi Gras Organization?: $previousMember \n".
		"If yes which one and why did you leave?: $whyLeftPrevious \n".
		"If accepted into the Krewe, what special skills do you bring to the Krewe?: $specialSkills \n".
		"I agree to the terms of the application: $agreeToTerms \n".
		"Sponsers: \n
		$sponser1 \n
		$sponser2 \n
		$sponser3 \n
		$sponser4 \n
		$sponser5 \n".
		"\n -------------------------------------- \n".
		"If you are experiencing any problems with form submissions, please contact me";
		mail($recipient, "Online application from kreweofpetronius.org", $emailText);
        $show_form=false;
		echo "Thank you, your application has been submited.<br />";
		echo "Questions? Contact applications@kreweofpetronius.org<br />";
		echo "Please <a href='http://www.shop.kreweofpetronius.org/Donate-50-to-Support-Krewe-of-Petronius-donate.htm'>click here</a> to submit your application fee online.<br />";
    }
    else
    {
		echo "<B>The following problems were encountered with your submission:</B>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
            echo "<p>$inp_err</p>\n";
        }        
    }
}

if(true == $show_form)
{
?>
and then here's the actions for the form itself:

Code: Select all

  <form id="ApplicationForm" name="ApplicationForm" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
I hope this isn't information overload. I also hope this code is salvageable since I was under the assumption I was finished with this project for now.

Thank you everyone for your help. If the answer is to throw this out and start over, at least break it to me gently! lol

Re: Mail form only sometimes works

Posted: Sun Jan 23, 2011 2:04 pm
by Jade
From what you're describing something being passed into the form validator is not working correctly.There's no way we can help you find the problem without seeing the contents of formvalidator.php. Can you post the code?

Re: Mail form only sometimes works

Posted: Sun Jan 23, 2011 6:54 pm
by Toubab
Yea I figured... I should have just included this at first but here it is:

Code: Select all

<?PHP
/*
  -------------------------------------------------------------------------
		      PHP Form Validator (formvalidator.php)
              Version 1.0
	Copyright (C) 2008 html-form-guide.com. All rights reserved.
	You can freely use this script.
	You may adapt this script for your own needs, provided these opening credit
    lines are kept intact.
		
	This Form validation script is distributed free from html-form-guide.com
	For updates, please visit:
	http://www.html-form-guide.com/php-form/php-form-validation.php
	
	Questions & comments please send to support@html-form-guide.com
  -------------------------------------------------------------------------  
*/

/**
* Carries information about each of the form validations
*/
class ValidatorObj
{
	var $variable_name;
	var $validator_string;
	var $error_string;
}

/**
* Base class for custom validation objects
**/
class CustomValidator 
{
	function DoValidate(&$formars,&$error_hash)
	{
		return true;
	}
}

/** Default error messages*/
define("E_VAL_REQUIRED_VALUE","Please enter the value for %s");
define("E_VAL_MAXLEN_EXCEEDED","Maximum length exceeded for %s.");
define("E_VAL_MINLEN_CHECK_FAILED","Please enter input with length more than %d for %s");
define("E_VAL_ALNUM_CHECK_FAILED","Please provide an alpha-numeric input for %s");
define("E_VAL_ALNUM_S_CHECK_FAILED","Please provide an alpha-numeric input for %s");
define("E_VAL_NUM_CHECK_FAILED","Please provide numeric input for %s");
define("E_VAL_ALPHA_CHECK_FAILED","Please provide alphabetic input for %s");
define("E_VAL_ALPHA_S_CHECK_FAILED","Please provide alphabetic input for %s");
define("E_VAL_EMAIL_CHECK_FAILED","Please provide a valida email address");
define("E_VAL_LESSTHAN_CHECK_FAILED","Enter a value less than %f for %s");
define("E_VAL_GREATERTHAN_CHECK_FAILED","Enter a value greater than %f for %s");
define("E_VAL_REGEXP_CHECK_FAILED","Please provide a valid input for %s");
define("E_VAL_DONTSEL_CHECK_FAILED","Wrong option selected for %s");
define("E_VAL_SELMIN_CHECK_FAILED","Please select minimum %d options for %s");
define("E_VAL_SELONE_CHECK_FAILED","Please select an option for %s");
define("E_VAL_EQELMNT_CHECK_FAILED","Value of %s should be same as that of %s");
define("E_VAL_NEELMNT_CHECK_FAILED","Value of %s should not be same as that of %s");



/**
* FormValidator: The main class that does all the form validations
**/
class FormValidator 
{
	var $validator_array;
    var $error_hash;
	var $custom_validators;
	
	function FormValidator()
	{
		$this->validator_array = array();
        $this->error_hash = array();
		$this->custom_validators=array();
	}
	
	function AddCustomValidator(&$customv)
	{
		array_push($this->custom_validators,$customv);
	}

	function addValidation($variable,$validator,$error)
	{
		$validator_obj = new ValidatorObj();
		$validator_obj->variable_name = $variable;
		$validator_obj->validator_string = $validator;
		$validator_obj->error_string = $error;
		array_push($this->validator_array,$validator_obj);
	}
    function GetErrors()
    {
        return $this->error_hash;
    }

	function ValidateForm()
	{
		$bret = true;

		$error_string="";
		$error_to_display = "";

        
		if(strcmp($_SERVER['REQUEST_METHOD'],'POST')==0)
		{
			$form_variables = $_POST;
		}
		else
		{
			$form_variables = $_GET;
		}

        $vcount = count($this->validator_array);
        

		foreach($this->validator_array as $val_obj)
		{
			if(!$this->ValidateObject($val_obj,$form_variables,$error_string))
			{
				$bret = false;
                $this->error_hash[$val_obj->variable_name] = $error_string;
			}
		}

		if(true == $bret && count($this->custom_validators) > 0)
		{
            foreach( $this->custom_validators as $custom_val)
			{
				if(false == $custom_val->DoValidate($form_variables,$this->error_hash))
				{
					$bret = false;
				}
			}
		}
		return $bret;
	}


	function ValidateObject($validatorobj,$formvariables,&$error_string)
	{
		$bret = true;

		$splitted = explode("=",$validatorobj->validator_string);
		$command = $splitted[0];
		$command_value = '';

		if(isset($splitted[1]) && strlen($splitted[1])>0)
		{
			$command_value = $splitted[1];
		}

		$default_error_message="";
		
		$input_value ="";

		if(isset($formvariables[$validatorobj->variable_name]))
		{
		 $input_value = $formvariables[$validatorobj->variable_name];
		}

		$bret = $this->ValidateCommand($command,$command_value,$input_value,
									$default_error_message,
									$validatorobj->variable_name,
									$formvariables);

		
		if(false == $bret)
		{
			if(isset($validatorobj->error_string) &&
				strlen($validatorobj->error_string)>0)
			{
				$error_string = $validatorobj->error_string;
			}
			else
			{
				$error_string = $default_error_message;
			}

		}//if
		return $bret;
	}
    	
	function validate_req($input_value, &$default_error_message,$variable_name)
	{
	  $bret = true;
      	if(!isset($input_value) ||
			strlen($input_value) <=0)
		{
			$bret=false;
			$default_error_message = sprintf(E_VAL_REQUIRED_VALUE,$variable_name);
		}	
	  return $bret;	
	}

	function validate_maxlen($input_value,$max_len,$variable_name,&$default_error_message)
	{
		$bret = true;
		if(isset($input_value) )
		{
			$input_length = strlen($input_value);
			if($input_length > $max_len)
			{
				$bret=false;
				$default_error_message = sprintf(E_VAL_MAXLEN_EXCEEDED,$variable_name);
			}
		}
		return $bret;
	}

	function validate_minlen($input_value,$min_len,$variable_name,&$default_error_message)
	{
		$bret = true;
		if(isset($input_value) )
		{
			$input_length = strlen($input_value);
			if($input_length < $min_len)
			{
				$bret=false;
				$default_error_message = sprintf(E_VAL_MINLEN_CHECK_FAILED,$min_len,$variable_name);
			}
		}
		return $bret;
	}

	function test_datatype($input_value,$reg_exp)
	{
		if(ereg($reg_exp,$input_value))
		{
			return false;
		}
		return true;
	}

	function validate_email($email) 
	{
		return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
	}

	function validate_for_numeric_input($input_value,&$validation_success)
	{
		
		$more_validations=true;
		$validation_success = true;
		if(strlen($input_value)>0)
		{
			
			if(false == is_numeric($input_value))
			{
				$validation_success = false;
				$more_validations=false;
			}
		}
		else
		{
			$more_validations=false;
		}
		return $more_validations;
	}

	function validate_lessthan($command_value,$input_value,
                $variable_name,&$default_error_message)
	{
		$bret = true;
		if(false == $this->validate_for_numeric_input($input_value,
                                    $bret))
		{
			return $bret;
		}
		if($bret)
		{
			$lessthan = doubleval($command_value);
			$float_inputval = doubleval($input_value);
			if($float_inputval >= $lessthan)
			{
				$default_error_message = sprintf(E_VAL_LESSTHAN_CHECK_FAILED,
										$lessthan,
										$variable_name);
				$bret = false;
			}//if
		}
		return $bret ;
	}

	function validate_greaterthan($command_value,$input_value,$variable_name,&$default_error_message)
	{
		$bret = true;
		if(false == $this->validate_for_numeric_input($input_value,$bret))
		{
			return $bret;
		}
		if($bret)
		{
			$greaterthan = doubleval($command_value);
			$float_inputval = doubleval($input_value);
			if($float_inputval <= $greaterthan)
			{
				$default_error_message = sprintf(E_VAL_GREATERTHAN_CHECK_FAILED,
										$greaterthan,
										$variable_name);
				$bret = false;
			}//if
		}
		return $bret ;
	}

    function validate_select($input_value,$command_value,&$default_error_message,$variable_name)
    {
	    $bret=false;
		if(is_array($input_value))
		{
			foreach($input_value as $value)
			{
				if($value == $command_value)
				{
					$bret=true;
					break;
				}
			}
		}
		else
		{
			if($command_value == $input_value)
			{
				$bret=true;
			}
		}
        if(false == $bret)
        {
            $default_error_message = sprintf(E_VAL_SHOULD_SEL_CHECK_FAILED,
                                            $command_value,$variable_name);
        }
	    return $bret;
    }

	function validate_dontselect($input_value,$command_value,&$default_error_message,$variable_name)
	{
	   $bret=true;
		if(is_array($input_value))
		{
			foreach($input_value as $value)
			{
				if($value == $command_value)
				{
					$bret=false;
					$default_error_message = sprintf(E_VAL_DONTSEL_CHECK_FAILED,$variable_name);
					break;
				}
			}
		}
		else
		{
			if($command_value == $input_value)
			{
				$bret=false;
				$default_error_message = sprintf(E_VAL_DONTSEL_CHECK_FAILED,$variable_name);
			}
		}
	  return $bret;
	}



	function ValidateCommand($command,$command_value,$input_value,&$default_error_message,$variable_name,$formvariables)
	{
		$bret=true;
		switch($command)
		{
			case 'req':
						{
							$bret = $this->validate_req($input_value, $default_error_message,$variable_name);
							break;
						}

			case 'maxlen':
						{
							$max_len = intval($command_value);
							$bret = $this->validate_maxlen($input_value,$max_len,$variable_name,
												$default_error_message);
							break;
						}

			case 'minlen':
						{
							$min_len = intval($command_value);
							$bret = $this->validate_minlen($input_value,$min_len,$variable_name,
											$default_error_message);
							break;
						}

			case 'alnum':
						{
							$bret= $this->test_datatype($input_value,"[^A-Za-z0-9]");
							if(false == $bret)
							{
								$default_error_message = sprintf(E_VAL_ALNUM_CHECK_FAILED,$variable_name);
							}
							break;
						}

			case 'alnum_s':
						{
							$bret= $this->test_datatype($input_value,"[^A-Za-z0-9 ]");
							if(false == $bret)
							{
								$default_error_message = sprintf(E_VAL_ALNUM_S_CHECK_FAILED,$variable_name);
							}
							break;
						}

			case 'num':
            case 'numeric':
						{
							$bret= $this->test_datatype($input_value,"[^0-9]");
							if(false == $bret)
							{
								$default_error_message = sprintf(E_VAL_NUM_CHECK_FAILED,$variable_name);
							}
							break;							
						}

			case 'alpha':
						{
							$bret= $this->test_datatype($input_value,"[^A-Za-z]");
							if(false == $bret)
							{
								$default_error_message = sprintf(E_VAL_ALPHA_CHECK_FAILED,$variable_name);
							}
							break;
						}
			case 'alpha_s':
						{
							$bret= $this->test_datatype($input_value,"[^A-Za-z ]");
							if(false == $bret)
							{
								$default_error_message = sprintf(E_VAL_ALPHA_S_CHECK_FAILED,$variable_name);
							}
							break;
						}
			case 'email':
						{
							if(isset($input_value) && strlen($input_value)>0)
							{
								$bret= $this->validate_email($input_value);
								if(false == $bret)
								{
									$default_error_message = E_VAL_EMAIL_CHECK_FAILED;
								}
							}
							break;
						}
			case "lt": 
			case "lessthan": 
						{
							$bret = $this->validate_lessthan($command_value,
													$input_value,
													$variable_name,
													$default_error_message);
							break;
						}
			case "gt": 
			case "greaterthan": 
						{
							$bret = $this->validate_greaterthan($command_value,
													$input_value,
													$variable_name,
													$default_error_message);
							break;
						}

			case "regexp":
						{
							if(isset($input_value) && strlen($input_value)>0)
							{
								if(!preg_match("$command_value",$input_value))
								{
									$bret=false;
									$default_error_message = sprintf(E_VAL_REGEXP_CHECK_FAILED,$variable_name);
								}
							}
							break;
						}
		  case "dontselect": 
		  case "dontselectchk":
          case "dontselectradio":
						{
							$bret = $this->validate_dontselect($input_value,
															   $command_value,
															   $default_error_message,
																$variable_name);
							 break;
						}//case

          case "shouldselchk":
          case "selectradio":
                      {
                            $bret = $this->validate_select($input_value,
							       $command_value,
							       $default_error_message,
								    $variable_name);
                            break;
                      }//case
		  case "selmin":
						{
							$min_count = intval($command_value);

							if(isset($input_value))
                            {
							    if($min_count > 1)
							    {
							        $bret = (count($input_value) >= $min_count )?true:false;
							    }
                                else
                                {
                                  $bret = true;
                                }
                            }
							else
							{
								$bret= false;
								$default_error_message = sprintf(E_VAL_SELMIN_CHECK_FAILED,$min_count,$variable_name);
							}

							break;
						}//case
		 case "selone":
						{
							if(false == isset($input_value)||
								strlen($input_value)<=0)
							{
								$bret= false;
								$default_error_message = sprintf(E_VAL_SELONE_CHECK_FAILED,$variable_name);
							}
							break;
						}
		 case "eqelmnt":
						{

							if(isset($formvariables[$command_value]) &&
							   strcmp($input_value,$formvariables[$command_value])==0 )
							{
								$bret=true;
							}
							else
							{
								$bret= false;
								$default_error_message = sprintf(E_VAL_EQELMNT_CHECK_FAILED,$variable_name,$command_value);
							}
						break;
						}
		  case "neelmnt":
						{
							if(isset($formvariables[$command_value]) &&
							   strcmp($input_value,$formvariables[$command_value]) !=0 )
							{
								$bret=true;
							}
							else
							{
								$bret= false;
								$default_error_message = sprintf(E_VAL_NEELMNT_CHECK_FAILED,$variable_name,$command_value);
							}
							break;
						}
		 
		}//switch
		return $bret;
	}//validdate command


}
/*
	Copyright (C) 2008 html-form-guide.com . All rights reserved.
*/
?> 

Re: Mail form only sometimes works

Posted: Mon Jan 31, 2011 7:37 am
by Jade
It looks like your form validator is working okay. Try running a stripslashes on your email text before you send it.

Code: Select all

    if($validator->ValidateForm())
    {
        // successful submit actions
                //$recipient = "applications@kreweofpetronius.org"; //email for the organization
                $recipient = "xxxx@gmail.com"; //my email for testing
                $emailText = stripslashes(
                "$applicantName has submitted an online application.  Here is their information:\n\n".
                "Name: $applicantName \n".
                "Address: $addressLine1 \n".
                "$addressLine2 \n".
                "$city, $state $zipCode \n".
                "Phone: $homePhone \n".
                "Cell Phone: $cellPhone \n".
                "Email: $applicantEmail \n".
                "I would like to become a member of the Krewe of Petronius because: $beMemberBecause \n".
                "Have you ever been a member of the Krewe of Petronius or any other gay Mardi Gras Organization?: $previousMember \n".
                "If yes which one and why did you leave?: $whyLeftPrevious \n".
                "If accepted into the Krewe, what special skills do you bring to the Krewe?: $specialSkills \n".
                "I agree to the terms of the application: $agreeToTerms \n".
                "Sponsers: \n
                $sponser1 \n
                $sponser2 \n
                $sponser3 \n
                $sponser4 \n
                $sponser5 \n".
                "\n -------------------------------------- \n".
                "If you are experiencing any problems with form submissions, please contact me");