2012-11-15 53 views
0

我無法讓我的PHP文件運行時,當我點擊提交時,我有自定義驗證。當我在我的下拉框中選擇其他內容時,會出現一個文本框。一旦我提交,它將不會運行我的PHP文件。如果我在我的下拉列表中選擇了一個沒有這個自定義驗證的值,我的php文件將運行PHP文件將無法運行,因爲驗證HTML格式

<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: [email protected] 
BODY; 
//End of Email body 

// single email address 

$to = "[email protected]"; 
$to2 = "[email protected]"; 


// multiple recipients 
//$to = '[email protected]' 

//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; 
     } 
} 

?> 
+0

什麼'定製validation'三元你在說什麼,有你有沒有貼出更多的JavaScript? – jeroen

回答

1

檢查,看看頁面拋出任何JavaScript錯誤。我可以在您的onChange事件中看到錯誤 - 在if上丟失右括號。儘量

<select name="TypeOfLeave" id="TypeOfLeave" 
    onchange=" 
     if(this.value == 'Other') { 
      this.form['Other'].style.visibility = 'visible'; 
     } else { 
      this.form['Other'].style.visibility = 'hidden'; 
     }"> 

你也可以使用,如果

onchange="this.form['Other'].style.visibility = (this.value == 'Other') ? 'visible': 'hidden';" 
+0

爲什麼不直接在onchange事件中調用函數?這樣它會更乾淨;而該函數的其餘部分寫入一個js文件 – ianace