We help IT Professionals succeed at work.

php form submitting on load

robdijo
robdijo asked
on
I have created a php form.  The file is on the web server as form.php

I have the html form first and then the php code for validation in the file.

In other words the web page refers to itself when doing the form.

When the page is loaded the form is executing and sending the email but it is all blank.  The form works when you fill in the information after that.  The form is in working order but I need it to stop sending that first blank form when loading.

Thanks - I have attached my code.
<?php

//  IF WE WANT TO TURN ON ERROR REPORTING
//ini_set('display_errors', 1);
//ini_set('log_errors', 1);
//ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
//error_reporting(E_ALL);

if (isset($_POST['submit']))  {

$errors = array();

    if ($_POST['employees'] == "over10")  {
	$errors[]='Too many employees - this client does not meet the criteria for this form.';
	}
	
	if ($_POST['gross'] > "2000000")  {
	$errors[]='Gross receipts over 2 million - this client does not meet the criteria for this form.';
	}
	
	if ($_POST['sub'] == "more25")  {
	$errors[]='More than 25% subcontracted - this client does not meet the criteria for this form.';
	}
	
	if ($_POST['volts'] == "more440")  {
	$errors[]='More than 440 volts - this client does not meet the criteria for this form.';
	}


	if (!empty($_POST['name']))  {
	$name=$_POST['name'];
	} else {
	$errors[]='Please enter your name';
	}

	if (!empty($_POST['email']))  {
	$email=$_POST['email'];
	} else {
	$errors[]='Please enter your email';
	}
	
	if (!empty($_POST['client']))  {
	$client=$_POST['client'];
	} else {
	$errors[]='Please enter the client name';
	}
	
	if (!empty($_POST['web']))  {
	$web=$_POST['web'];
	} else {
	$errors[]='Please enter the client web site - If none then type NONE';
	}
	
	if (!empty($_POST['years']))  {
	$years=$_POST['years'];
	} else {
	$errors[]='Please enter the number of years this client has been in business';
	}

	if ($_POST['employees'] != "null")  {
	$employees=$_POST['employees'];
	} else {
	$errors[]='Please select how many employees';
	}

	if (!empty($_POST['gross']))  {
	$gross=$_POST['gross'];
	} else {
	$errors[]='Please enter the gross receipts';
	}

	if ($_POST['sub'] != "null")  {
	$sub=$_POST['sub'];
	} else {
	$errors[]='Please select the percentage of work subcontracted';
	}
	
	if (!empty($_POST['operation']))  {
	$operation=$_POST['operation'];
	} else {
	$errors[]='Please enter the applicant description of operations';
	}
	
	if (!empty($_POST['stories']))  {
	$stories=$_POST['stories'];
	} else {
	$errors[]='Please enter the maximum height of operations';
	}
	
	if ($_POST['volts'] != "null")  {
	$volts=$_POST['volts'];
	} else {
	$errors[]='Please select if the contractor uses less than or more than 440 volts';
	}
			
	if (!empty($_POST['residential']))  {
	$residential=$_POST['residential'];
	} else {
	$errors[]='Please enter the percentage of residential work - Enter 0 if none';
	}
	
	if (!empty($_POST['commercial']))  {
	$commercial=$_POST['commercial'];
	} else {
	$errors[]='Please enter the percentage of commercial work - Enter 0 if none';
	}
	
	$res=$_POST['residential'];
	$comm=$_POST['commercial'];
	if ($res + $comm == 100) {
	$rescomm=0;
	} else {
	$errors[]='Residential and Commercial do not equal 100 percent';
	}
	
	if ($_POST['PPE'] != "null")  {
	$PPE=$_POST['PPE'];
	} else {
	$errors[]='Please select if the contractor provides PPE';
	}
	
	if ($_POST['bulky'] != "null")  {
	$bulky=$_POST['bulky'];
	} else {
	$errors[]='Please select if the contractor provides equipment for heavy items';
	}
	
	if ($_POST['lifting'] != "null")  {
	$lifting=$_POST['lifting'];
	} else {
	$errors[]='Please select if the employees are trained in proper lifting techniques';
	}
	
	if (!empty($_POST['subbed']))  {
	$subbed=$_POST['subbed'];
	} else {
	$errors[]='Please enter the specifics concerning the subcontractors used';
	}
	
	if ($_POST['radius'] != "null")  {
	$radius=$_POST['radius'];
	} else {
	$errors[]='Please select the radius of operation';
	}
	
	if ($_POST['screen'] != "null")  {
	$screen=$_POST['screen'];
	} else {
	$errors[]='Please select if the contractor screens employees';
	}
	
	if ($_POST['special'] != "null")  {
	$special=$_POST['special'];
	} else {
	$errors[]='Please select if the contractor requires a special federal license';
	
}}
	
	
	
if (!empty($errors))  {
	foreach ($errors as $msg)  {
	echo "<p><b><font color='red'>$msg</font></b></p>";
	} 
        } else {
			
	$headers="From: $email" . "\r\n" . "Reply-to: $email";
		
	$body="Agent --> $name\n\n\nAgent Email --> $email\n\n\nClient Name --> $client\n\n\nClient Web Site --> $web\n\n\nNumber Of Years In Business --> $years\n\n\nNumber Of Employees --> $employees\n\n\nGross Receipts --> $gross\n\n\nPercentage of Subbed Work -->$sub\n\n\nDescription Of Operations -->\n\n\n$operation\n\n\nWhat is the maximum height of applicant's operations (in stories)? --> $stories\n\n\nVolts Worked With --> $volts\n\n\nPercent Residential --> $residential\n\n\nPercent Commercial --> $commercial\n\n\nDoes applicant provide PPE (gloves, boots and eye wear) to employees and mandate its use? --> $PPE\n\n\nAre hand trucks and other equipment provided for moving bulky or heavy items? --> $bulky\n\n\nHave employees been trained in proper lifting techniques and in the correct use of material handling devices? --> $lifting\n\n\nDescriptions Of Subbed Operations -->\n\n\n$subbed\n\n\nRadius Of Operation --> $radius miles\n\n\nDoes applicant screen drivers (check MVRs pre-employment and annually thereafter)? --> $screen\n\n\nDoes applicant require USL&H or other federal coverage (Jones Act, etc.)? --> $special";

    mail ('info@mail.com', 'Electrical Contractor Form Submitted', $body, $headers);
    echo "<p><font color='blue'>Thank you for your submission!</font></p>";
		
		

}

	


?>

Open in new window

Comment
Watch Question

Fixer of Problems
Most Valuable Expert 2014
Commented:
The 'if errors' section needs to be inside the the 'if(isset...submit)' section like below.
<?php

//  IF WE WANT TO TURN ON ERROR REPORTING
//ini_set('display_errors', 1);
//ini_set('log_errors', 1);
//ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
//error_reporting(E_ALL);

if (isset($_POST['submit']))  {

$errors = array();

    if ($_POST['employees'] == "over10")  {
	$errors[]='Too many employees - this client does not meet the criteria for this form.';
	}
	
	if ($_POST['gross'] > "2000000")  {
	$errors[]='Gross receipts over 2 million - this client does not meet the criteria for this form.';
	}
	
	if ($_POST['sub'] == "more25")  {
	$errors[]='More than 25% subcontracted - this client does not meet the criteria for this form.';
	}
	
	if ($_POST['volts'] == "more440")  {
	$errors[]='More than 440 volts - this client does not meet the criteria for this form.';
	}


	if (!empty($_POST['name']))  {
	$name=$_POST['name'];
	} else {
	$errors[]='Please enter your name';
	}

	if (!empty($_POST['email']))  {
	$email=$_POST['email'];
	} else {
	$errors[]='Please enter your email';
	}
	
	if (!empty($_POST['client']))  {
	$client=$_POST['client'];
	} else {
	$errors[]='Please enter the client name';
	}
	
	if (!empty($_POST['web']))  {
	$web=$_POST['web'];
	} else {
	$errors[]='Please enter the client web site - If none then type NONE';
	}
	
	if (!empty($_POST['years']))  {
	$years=$_POST['years'];
	} else {
	$errors[]='Please enter the number of years this client has been in business';
	}

	if ($_POST['employees'] != "null")  {
	$employees=$_POST['employees'];
	} else {
	$errors[]='Please select how many employees';
	}

	if (!empty($_POST['gross']))  {
	$gross=$_POST['gross'];
	} else {
	$errors[]='Please enter the gross receipts';
	}

	if ($_POST['sub'] != "null")  {
	$sub=$_POST['sub'];
	} else {
	$errors[]='Please select the percentage of work subcontracted';
	}
	
	if (!empty($_POST['operation']))  {
	$operation=$_POST['operation'];
	} else {
	$errors[]='Please enter the applicant description of operations';
	}
	
	if (!empty($_POST['stories']))  {
	$stories=$_POST['stories'];
	} else {
	$errors[]='Please enter the maximum height of operations';
	}
	
	if ($_POST['volts'] != "null")  {
	$volts=$_POST['volts'];
	} else {
	$errors[]='Please select if the contractor uses less than or more than 440 volts';
	}
			
	if (!empty($_POST['residential']))  {
	$residential=$_POST['residential'];
	} else {
	$errors[]='Please enter the percentage of residential work - Enter 0 if none';
	}
	
	if (!empty($_POST['commercial']))  {
	$commercial=$_POST['commercial'];
	} else {
	$errors[]='Please enter the percentage of commercial work - Enter 0 if none';
	}
	
	$res=$_POST['residential'];
	$comm=$_POST['commercial'];
	if ($res + $comm == 100) {
	$rescomm=0;
	} else {
	$errors[]='Residential and Commercial do not equal 100 percent';
	}
	
	if ($_POST['PPE'] != "null")  {
	$PPE=$_POST['PPE'];
	} else {
	$errors[]='Please select if the contractor provides PPE';
	}
	
	if ($_POST['bulky'] != "null")  {
	$bulky=$_POST['bulky'];
	} else {
	$errors[]='Please select if the contractor provides equipment for heavy items';
	}
	
	if ($_POST['lifting'] != "null")  {
	$lifting=$_POST['lifting'];
	} else {
	$errors[]='Please select if the employees are trained in proper lifting techniques';
	}
	
	if (!empty($_POST['subbed']))  {
	$subbed=$_POST['subbed'];
	} else {
	$errors[]='Please enter the specifics concerning the subcontractors used';
	}
	
	if ($_POST['radius'] != "null")  {
	$radius=$_POST['radius'];
	} else {
	$errors[]='Please select the radius of operation';
	}
	
	if ($_POST['screen'] != "null")  {
	$screen=$_POST['screen'];
	} else {
	$errors[]='Please select if the contractor screens employees';
	}
	
	if ($_POST['special'] != "null")  {
	$special=$_POST['special'];
	} else {
	$errors[]='Please select if the contractor requires a special federal license';
	
}
	
	
	
if (!empty($errors))  {
	foreach ($errors as $msg)  {
	echo "<p><b><font color='red'>$msg</font></b></p>";
	} 
        } else {
			
	$headers="From: $email" . "\r\n" . "Reply-to: $email";
		
	$body="Agent --> $name\n\n\nAgent Email --> $email\n\n\nClient Name --> $client\n\n\nClient Web Site --> $web\n\n\nNumber Of Years In Business --> $years\n\n\nNumber Of Employees --> $employees\n\n\nGross Receipts --> $gross\n\n\nPercentage of Subbed Work -->$sub\n\n\nDescription Of Operations -->\n\n\n$operation\n\n\nWhat is the maximum height of applicant's operations (in stories)? --> $stories\n\n\nVolts Worked With --> $volts\n\n\nPercent Residential --> $residential\n\n\nPercent Commercial --> $commercial\n\n\nDoes applicant provide PPE (gloves, boots and eye wear) to employees and mandate its use? --> $PPE\n\n\nAre hand trucks and other equipment provided for moving bulky or heavy items? --> $bulky\n\n\nHave employees been trained in proper lifting techniques and in the correct use of material handling devices? --> $lifting\n\n\nDescriptions Of Subbed Operations -->\n\n\n$subbed\n\n\nRadius Of Operation --> $radius miles\n\n\nDoes applicant screen drivers (check MVRs pre-employment and annually thereafter)? --> $screen\n\n\nDoes applicant require USL&H or other federal coverage (Jones Act, etc.)? --> $special";

    mail ('info@mail.com', 'Electrical Contractor Form Submitted', $body, $headers);
    echo "<p><font color='blue'>Thank you for your submission!</font></p>";
		
		

}
}
	


?>

Open in new window

Commented:
Try this.

<?php

//  IF WE WANT TO TURN ON ERROR REPORTING
//ini_set('display_errors', 1);
//ini_set('log_errors', 1);
//ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
//error_reporting(E_ALL);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	if (isset($_POST['submit']))  {
		$errors = array();

		if ($_POST['employees'] == "over10")  {
        	$errors[]='Too many employees - this client does not meet the criteria for this form.';
        }
        
        if ($_POST['gross'] > "2000000")  {
        	$errors[]='Gross receipts over 2 million - this client does not meet the criteria for this form.';
        }
        
        if ($_POST['sub'] == "more25")  {
        	$errors[]='More than 25% subcontracted - this client does not meet the criteria for this form.';
        }
        
        if ($_POST['volts'] == "more440")  {
        	$errors[]='More than 440 volts - this client does not meet the criteria for this form.';
        }


        if (!empty($_POST['name']))  {
			$name=$_POST['name'];
        } else {
        	$errors[]='Please enter your name';
        }

        if (!empty($_POST['email']))  {
			$email=$_POST['email'];
        } else {
        	$errors[]='Please enter your email';
        }
        
        if (!empty($_POST['client']))  {
			$client=$_POST['client'];
        } else {
        	$errors[]='Please enter the client name';
        }
        
        if (!empty($_POST['web']))  {
			$web=$_POST['web'];
        } else {
        	$errors[]='Please enter the client web site - If none then type NONE';
        }
        
        if (!empty($_POST['years']))  {
			$years=$_POST['years'];
        } else {
        	$errors[]='Please enter the number of years this client has been in business';
        }

        if ($_POST['employees'] != "null")  {
			$employees=$_POST['employees'];
        } else {
        	$errors[]='Please select how many employees';
        }

        if (!empty($_POST['gross']))  {
			$gross=$_POST['gross'];
        } else {
        	$errors[]='Please enter the gross receipts';
        }

        if ($_POST['sub'] != "null")  {
			$sub=$_POST['sub'];
        } else {
        	$errors[]='Please select the percentage of work subcontracted';
        }
        
        if (!empty($_POST['operation']))  {
			$operation=$_POST['operation'];
        } else {
        	$errors[]='Please enter the applicant description of operations';
        }
        
        if (!empty($_POST['stories']))  {
			$stories=$_POST['stories'];
        } else {
        	$errors[]='Please enter the maximum height of operations';
        }
        
        if ($_POST['volts'] != "null")  {
			$volts=$_POST['volts'];
        } else {
        	$errors[]='Please select if the contractor uses less than or more than 440 volts';
        }
                        
        if (!empty($_POST['residential']))  {
			$residential=$_POST['residential'];
        } else {
        	$errors[]='Please enter the percentage of residential work - Enter 0 if none';
        }
        
        if (!empty($_POST['commercial']))  {
			$commercial=$_POST['commercial'];
        } else {
        	$errors[]='Please enter the percentage of commercial work - Enter 0 if none';
        }
        
        $res=$_POST['residential'];
        $comm=$_POST['commercial'];
        if ($res + $comm == 100) {
			$rescomm=0;
        } else {
        	$errors[]='Residential and Commercial do not equal 100 percent';
        }
        
        if ($_POST['PPE'] != "null")  {
			$PPE=$_POST['PPE'];
        } else {
        	$errors[]='Please select if the contractor provides PPE';
        }
        
        if ($_POST['bulky'] != "null")  {
			$bulky=$_POST['bulky'];
        } else {
        	$errors[]='Please select if the contractor provides equipment for heavy items';
        }
        
        if ($_POST['lifting'] != "null")  {
			$lifting=$_POST['lifting'];
        } else {
        	$errors[]='Please select if the employees are trained in proper lifting techniques';
        }
        
        if (!empty($_POST['subbed']))  {
			$subbed=$_POST['subbed'];
        } else {
        	$errors[]='Please enter the specifics concerning the subcontractors used';
        }
        
        if ($_POST['radius'] != "null")  {
			$radius=$_POST['radius'];
        } else {
        	$errors[]='Please select the radius of operation';
        }
        
        if ($_POST['screen'] != "null")  {
			$screen=$_POST['screen'];
        } else {
        	$errors[]='Please select if the contractor screens employees';
        }
        
        if ($_POST['special'] != "null")  {
			$special=$_POST['special'];
        } else {
        	$errors[]='Please select if the contractor requires a special federal license';
        }

		if (!empty($errors))  {
			foreach ($errors as $msg)  {
				echo "<p><b><font color='red'>$msg</font></b></p>";
			} 
		} else {
							
			$headers="From: $email" . "\r\n" . "Reply-to: $email";
					
			$body="Agent --> $name\n\n\nAgent Email --> $email\n\n\nClient Name --> $client\n\n\nClient Web Site --> $web\n\n\nNumber Of Years In Business --> $years\n\n\nNumber Of Employees --> $employees\n\n\nGross Receipts --> $gross\n\n\nPercentage of Subbed Work -->$sub\n\n\nDescription Of Operations -->\n\n\n$operation\n\n\nWhat is the maximum height of applicant's operations (in stories)? --> $stories\n\n\nVolts Worked With --> $volts\n\n\nPercent Residential --> $residential\n\n\nPercent Commercial --> $commercial\n\n\nDoes applicant provide PPE (gloves, boots and eye wear) to employees and mandate its use? --> $PPE\n\n\nAre hand trucks and other equipment provided for moving bulky or heavy items? --> $bulky\n\n\nHave employees been trained in proper lifting techniques and in the correct use of material handling devices? --> $lifting\n\n\nDescriptions Of Subbed Operations -->\n\n\n$subbed\n\n\nRadius Of Operation --> $radius miles\n\n\nDoes applicant screen drivers (check MVRs pre-employment and annually thereafter)? --> $screen\n\n\nDoes applicant require USL&H or other federal coverage (Jones Act, etc.)? --> $special";

			mail ('info@mail.com', 'Electrical Contractor Form Submitted', $body, $headers);
			echo "<p><font color='blue'>Thank you for your submission!</font></p>";
		}					
	} else {
		// no $_POST['submit'] found
		// do whatever you want here
	}
} else {
	// page is requested by GET
?>
<form method="post" action="">
<!-- Show your form here -->
</form>
<?php
}
?>

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Usually the design of form+action scripts puts the action script at the top of the script file and the form part at the bottom of the script file, like this.
http://www.laprbass.com/RAY_post_example.php
<?php // RAY_post_example.php
error_reporting(E_ALL);
echo "<pre>"; // MAKE IT EASY TO READ



// THIS ILLUSTRATES THE RELATIONSHIP BETWEEN THE 'ACTION' AND THE 'FORM' WHEN COMBINED INTO A SINGLE PHP SCRIPT FILE



// SET A DEFAULT VALUES FOR A VARIABLE TO PREPOPULATE THE FORM - THIS COULD COME FROM A DATA BASE, OR MIGHT BE EMPTY
$my_input_Field = 'ORIGINAL DATA';



// HAS ANYTHING BEEN POSTED? IF SO, $_POST IS SET AND CONTAINS THE DATA
if (!empty($_POST))
{
    // THIS IS THE ACTION SCRIPT.  IN THIS EXAMPLE CODE, ALL WE DO IS SHOW THE POST ARRAY
    var_dump($_POST);

    // IF THE FORM WAS FILLED IN, COPY THE INPUT INTO OUR VARIABLE
    if (!empty($_POST["my_input_Field"]))
    {
        $my_input_Field = $_POST["my_input_Field"];
    }

    // THIS IS THE END OF THE ACTION SCRIPT
    echo 'ACTION COMPLETED';
}



// THIS IS THE FORM SCRIPT CREATED IN HEREDOC NOTATION
$form = <<<ENDFORM
<form method="post">
TYPE SOMETHING HERE:
<input type="text"   name="my_input_Field"   value="$my_input_Field" />
<input type="hidden" name="my_HIDDEN_Field"  value="Not Really Much of a Secret" />
<input type="submit" name="my_SUBMIT_Button" value="go" />
</form>
ENDFORM;

// WRITE THE FORM TO THE BROWSER
echo $form;

Open in new window

Abhijeet RananawareWeb & Mobile Developer

Commented:
DaveBaldwin: is correct. Email code should be inside isset...

Regards,
Abhijit

Author

Commented:
Thanks!  I had two }} on line:157 when I should have had only one } on that line and two }} on line:176