2013-07-01 45 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)