当我有自定义验证时,当我点击提交时,我无法运行我的 php 文件。当我在下拉框中选择其他时,会出现一个文本框。一旦我点击提交,它将不会运行我的 php 文件。如果我在没有此自定义验证的情况下在下拉列表中选择一个值,我的 php 文件将运行。
<body>
<form action="form-to-email.php" method="post" name="LeaveRequestPart1" id="LeaveRequestPart1">
<table class="LeaveRequestPart1" align="center">
<tr>
<td colspan="2"><font color="red">*</font>
<label>Type of Leave</label>
<select name="TypeOfLeave" id="TypeOfLeave" onchange =
"if(this.value=='Other'{this.form['Other'].style.visibility='visible'}else {this.form['Other'].style.visibility='hidden'};">
<option value="N/A"></option>
<option value="Vacation">Vacation</option>
<option value="Illness">Illness</option>
<option value="FamilyIllness">Family Illness</option>
<option value="Bereavement">Bereavement</option>
<option value="PersonalWithPay">Personal - with pay</option>
<option value="PersonalWithoutPay">Personal - without pay</option>
<option value="JuryDuty">Jury Duty</option>
<option value="Military">Military</option>
<option value="Pallbearer">Pallbearer</option>
<option value="Professional" id="Professional">Professional</option>
<option value="Routine" id="Routine">Routine - overnight</option>
<option value="Other">Other</option>
</select>
<input type="text" name="Other" id="Other" style="visibility:hidden;" />
</td>
</tr>
</table>
</form>
</body>
</html>
PHP 代码
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "Form Error; You need to submit the form. ";
}
//Form Variables
$ID = $_POST['ID'];
$Name = $_POST['Name'];
$CampusLocation = $_POST['CampusLocation'];
$TypeOfRequest = $_POST['TypeOfRequest'];
$TypeOfLeave = $_POST['TypeOfLeave'];
$Other = $_POST['Other'];
//Validate first
if(IsInjected($SEmail))
{
echo "Bad email value!";
exit;
}
if(IsInjected($YEmail))
{
echo "Bad email value!";
exit;
}
$Email_subject = "Leave Request Submitted";
//Start of Email body. Do not remobe BODY commands
$Email_body = <<<BODY
Employee ID: $ID
Name: $Name
Campus: $CampusLocation
Type of Request: $TypeOfRequest
Type of Leave: $TypeOfLeave
Other Type of Leave: $Other
Employee CCC email address: $YEmail@cccneb.edu
BODY;
//End of Email body
// single email address
$to = "$SEmail@cccneb.edu";
$to2 = "$YEmail@cccneb.edu";
// multiple recipients
//$to = 'YEmail@cccneb.edu'
//Email From & Reply
$headers = "From: $to2 \r\n";
$headers .= "Reply-To: $to2 \r\n";
//Send the email!
mail($to,$Email_subject,$Email_body,$headers);
//done. redirect to thank-you page.
header('Location: form-end.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>