我有一个带有文本输入和文件上传的表单。我在下面编写的代码基本上可以工作(需要额外的功能,我将在一个单独的问题中询问)但我想知道是否有一种更简洁、更简洁的方式来编写它。我是 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
}
}
?>