2013-07-01 85 views
1

我是PHP新手,嘗試創建一個包含所有必填字段的表單,其中包括必須選擇文件的表單。這是我想達到的目標:PHP驗證問題 - 表單即使在必填字段爲空時提交

  • 用戶必須完成4場+上傳文件
  • 文件只能是某一類型+具有一定規模下
  • 如果用戶沒有完成的一個要求和點擊次數提交後,空字段
  • 旁邊將顯示「必填」一詞,如果選定的文件不符合標準,將出現不同的消息
  • 數據保存在填充的字段中,如果用戶離開空白,必須回去填寫。
  • 當表單提交時,信息進入數據庫+成電子郵件

我很接近但錯過了一些東西。如果我選擇了符合要求的文件,即使其他字段爲空,表單也會提交。只要表單域爲空,其他域的行爲就會正確。我錯過了什麼?我將不勝感激任何幫助。謝謝。

 <?php require_once('../scripts/lcoa.php'); ?> 
     <?php 
     if (isset($_GET['jobid'])) { 
     $jobid = $_GET['jobid']; 
     } 
     if (isset($_GET['jobtitle'])) { 
     $jobtitle = $_GET['jobtitle']; 
     } 
     //This is the directory where resumes will be saved 
     $timestamp = time(); 
     $folder = "../careers/resumes/"; 
     $resume = ($_FILES['resume']['name']); 
     $target = $folder.basename($timestamp.$_FILES['resume']['name']); 
     $type = ($_FILES['resume']['type']); 
     $extension = strtolower(substr($resume, strpos($resume, '.') + 1)); 
     $size = ($_FILES['resume']['size']); 
     $max_size = 3145728; 
     $name = ($_POST['name']); 
     $email = ($_POST['email']); 
     $phone = ($_POST['phone']); 
     $jobid = ($_POST['jobid']); 
     $jobtitle = ($_POST['jobtitle']); 
     $cover = ($_POST['coverletter']); 
     $error=array(); 


      if(isset($name)){ 
       if (empty ($name)){ 
      $error['name']="<p class='error'>Required </p>"; 

       } 
      } 
      if(isset($email)){ 
       if (empty ($email)){ 
      $error['email']="<p class='error'>Required </p>"; 
       } 
      } 
      if(isset($phone)){ 
        if (empty ($phone)){ 
       $error['phone']="<p class='error'>Required </p>"; 
        } 
       } 
      if(isset($cover)){ 
        if (empty ($cover)){ 
       $error['coverletter']="<p class='error'>Required </p>"; 
        } 
       } 
     //Writes the resume to the server 
     if (isset ($resume)) { 

      if (empty ($resume)){ 
       $error['resume']="<p class='error'>Resume Required </p>"; 
       } 

      if (!empty ($resume)){ 
       if(($extension=='doc'||$extension=='docx'||$extension=='txt'||$extension=='pdf')&&($type=='application/pdf'||'application/msword'||'application/vnd.openxmlformats-officedocument.wordprocessingml.document'||'text/plain')&&$size<=$max_size) { 
       if(move_uploaded_file($_FILES['resume']['tmp_name'], $target)) { 
        //Writes the information to the database 
       $insertSQL = "INSERT INTO applicants (id, name, email, phone, jobid, jobtitle, coverletter, resume) VALUES ('','".$_POST['name']."','".$_POST['email']."','".$_POST['phone']."','".$_POST['jobid']."','".$_POST['jobtitle']."','".$_POST['coverletter']."','".$resume."')"; 
       mysql_select_db($database_lcoa, $lcoa); 
       $Result1 = mysql_query($insertSQL, $lcoa) or die(mysql_error()); 
       //Sends Email 
        $sendto = "emailaddress"; 
        $name = nl2br($_POST['name']); 
        $email = nl2br($_POST['email']); 
        $phone = nl2br($_POST['phone']); 
        $jobid = nl2br($_POST['jobid']); 
        $jobtitle = nl2br($_POST['jobtitle']); 
        $cover = nl2br($_POST['coverletter']); 
        $subject = "Submitted Job Application"; 
        $headers .= "Content-Type: text/html;charset=utf-8 \r\n"; 
        $headers = "From: " . strip_tags($email) . "\r\n"; 
        $headers .= "Reply-To: ". strip_tags($email) . "\r\n"; 
        $headers .= "MIME-Version: 1.0\r\n"; 
        $headers .= "Content-Type: text/html;charset=utf-8 \r\n"; 
        $msg = "<html><body style='font-family:Arial,sans-serif;'>"; 
        $msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>Job Application Submitted</h2>\r\n"; 
        $msg .= "<p><strong>Applied for:</strong> ".$jobtitle."</p>\r\n"; 
        $msg .= "<p><strong>Job ID:</strong> ".$jobid."</p>\r\n"; 
        $msg .= "<p><strong>Applicant Name:</strong> ".$name."</p>\r\n"; 
        $msg .= "<p><strong>Email:</strong> ".$email."</p>\r\n"; 
        $msg .= "<p><strong>Phone:</strong> ".$phone."</p>\r\n"; 
        $msg .= "<p><strong>Cover Letter:</strong> ".$cover."</p>\r\n"; 
        $msg .= "<a href='http://domain.com/".$target."'>Download Resume</a>\r\n"; 
        $msg .= "</body></html>"; 
        if(@mail($sendto, $subject, $msg, $headers)) { 
         echo ""; 
        } else { 
         echo "false"; 
        } 
        //Tells you if its all ok 
        echo "<div id='confirm-app'><p>Thank you for submitting your application. Resumes submitted will be reviewed to determine qualifications that match our hiring needs.<br /><br /> If you are selected you will be contacted by a member of our recruiting team.</p><br /><br /><a href='../careers/job-postings.php'>Return to current opportunities</a></div>"; 
        } 
       }  
        else { 
        //Gives and error if its not 
        echo "<p style='color: #6D6E71; font-family: Arial,Helvetica,sans-serif; font-size: 13px;'>We accept resumes in <strong>.doc</strong>, <strong>.docx</strong>, <strong>.pdf</strong>, or <strong>.txt</strong> formats, 3MB or less. Please <a href='javascript:history.back(-1);'>go back</a> to upload a file that meets these requirements.<br /><br />If you continue to experience errors, please report them.</p>"; 
        die(); 
        } 
       } 
     }  
     ?> 
+1

您在代碼中添加錯誤,但是何時檢查'$ error'是否爲空? – Sergio

+0

OMG ...首先,您需要將HTML模板與PHP邏輯分開。其次:'mail()'現在可以按照您希望這個功能在你移動到另一個主機後立即工作。應該使用'phpMailer'或'swiftMailer'來代替。第三:你需要明確分開責任(編寫一個處理上傳的類,另一個用於數據驗證等)。您正在編寫無法維護的代碼以及充滿不良做法的代碼。認真。 – Yang

回答

1

您必須添加近if (!empty ($resume))一個條件,檢查你的$error陣列空,如果不爲空,然後打印錯誤,否則插入或電子郵件等

if (!empty ($resume) && empty($error)){ 
     //do your stuff 
    }else{ 
    //display errors 
    } 
0

您只是在測試,看是否if (!empty ($resume)){和在執行數據庫插入和電子郵件發送之前對文件的要求。你將不得不測試其他元素是否正確。由於您正在構建一個名爲$error的數組,因此您可以在執行數據庫插入和電子郵件之前測試以查看empty($error)