1

我是 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();
                     } 
                }
        }       
         ?>
4

2 回答 2

1

您必须在附近添加一个条件,如果不为空,则if (!empty ($resume))检查您的$error数组是否为空,然后打印错误,否则插入或发送电子邮件等

    if (!empty ($resume) && empty($error)){
      //do your stuff
    }else{
    //display errors
    }
于 2013-07-01T21:12:19.187 回答
0

在执行数据库插入和电子邮件发送之前,您只是在测试文件是否if (!empty ($resume)){以及对文件的要求。您还必须测试其他元素是否正确。由于您正在构建一个名为的数组,因此您可以在执行数据库插入和电子邮件之前进行$error测试以查看是否。empty($error)

于 2013-07-01T21:18:17.550 回答