2013-06-27 65 views
1

我有一個帶有文本輸入和文件上傳的表單。下面我寫的代碼基本上可以工作(需要額外的功能,我會在單獨的問題中詢問),但是我想知道是否有更簡潔,更簡潔的方式來編寫它。我是全新的PHP和任何幫助將不勝感激。多種輸入類型的PHP表單驗證

如果有人試圖提交表單並且字段爲空,或者如果選擇的文件類型不是正確的類型或大小,我想要顯示每個輸入的錯誤消息。在下面的代碼中,我顯示了一條獨特的消息,但是如果有某種方法可以讓代碼更好,那麼我可以說「必需」。

我試過使用jQuery驗證插件,無法讓它與文件上傳一起工作(這超出了我對jQuery/Ajax/PHP的理解),所以這是我想出的解決方案。

 <?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 
     $target = "../careers/resumes/"; 
     $target = $target . basename($_FILES['resume']['name']); 
     $resume=($_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']); 

     if(isset($name)){ 
      if (empty ($name)){ 
       ?> 
       <script type="text/javascript"> 
        $(document).ready(function() { 
         $('#applicant-name').before('<p class="error">Please provide your full name. </p>');   
        }); 
       </script> 
       <?php 
      } 
     } 
     if(isset($email)){ 
      if (empty ($email)){ 
       ?> 
       <script type="text/javascript"> 
        $(document).ready(function() { 
         $('#applicant-email').before('<p class="error">Please provide your email address. </p>');   
        }); 
       </script> 
       <?php 
      } 
     } 
     if(isset($phone)){ 
      if (empty ($phone)){ 
       ?> 
       <script type="text/javascript"> 
        $(document).ready(function() { 
         $('#applicant-phone').before('<p class="error">Please provide a phone number. </p>');   
        }); 
       </script> 
       <?php 
      } 
     } 

     //Writes the resume to the server 
     if (isset ($resume)) { 
      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()); 

        //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(); 
        } 
       } 
       else { 
        ?> 
         <script type="text/javascript"> 
          $(document).ready(function() { 
           $('#upload-resume').before('<p class="error">Please select a file to upload. </p>');   
          }); 
         </script> 
        <?php 
       } 
     }  
     ?> 
+0

據我所知,你應該把東西到變量只是爲了使它更加明顯它是什麼,你做的事。所以我會說不,雖然我確定有清理它的框架。然而,我通常不會打擾並接受PHP有時會有些混亂。 –

+0

請嘗試一些教程,如http://buildinternet.com/2008/12/how-to-validate-a-form-complete-with-error-messages-using-php-part-1/ http: //www.daniweb.com/web-development/php/threads/186170/php-file-upload-validation-before-the-file-uploads –

+0

@dianuj謝謝,它看起來像該教程的邏輯幾乎是什麼我做到了。我確實使用了各種教程來提出這個解決方案。 – surfbird0713

回答

0

只是給你一個想法的幾個字段用於其他領域的嘗試,通過你的自我

//php 
    $name=$_POST['name']; 
    $email=$_POST['email']; 
    $error=array(); 
    if(isset($name)){ 
     if (empty ($name)){ 
    $error['name']="<p class='error'>Please provide your full name. </p>"; 
     } 
    } 
    if(isset($email)){ 
     if (empty ($email)){ 
    $error['email']="<p class='error'>Please provide correct email. </p>"; 
     } 
    } 

    //HTML 

<form action="" name"myform"> 

<?php if(!empty($error['name'])){ echo $error['name']; } ?> 

<input type="text" name="name" value="<?php if(isset($name)){ echo $name; } ?>"/> 

<?php if(!empty($error['email'])){ echo $error['email']; } ?> 

<input type="text" name="name" value="<?php if(isset($name)){ echo $name; } ?>"/> 

</form> 

它只是一個想法告訴你相同的邏輯在驗證文件上傳或必須使用正則表達式測試對電話,電子郵件等

希望這是有道理的