我有一个包含文本输入和文件上传的表单。我在下面编写的代码基本上可以工作(需要额外的功能,我将在一个单独的问题中询问),但我想知道是否有更简洁,更简洁的方式来编写它。我是PHP的全新,任何帮助将不胜感激。
如果有人尝试提交表单且字段为空,或者所选文件类型的类型或大小不正确,我想在每个输入中显示错误消息。在下面的代码中我显示了一个独特的消息,但第二个想法我可以说“必需”,如果有一些方法可以通过这样做使这些代码更好。
我尝试使用jQuery validate插件并且无法使用它来处理文件上传(这超出了我对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 :(得分: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>
它只是一个想法,在验证文件上传时显示相同的逻辑,或者必须使用正则表达式测试手机,电子邮件等
希望它有意义