I have a form with text inputs and a file upload. The code I have written below basically works (needs additional functionality which I will ask about in a separate question) but I'm wondering if there is a cleaner, more succinct way to write this. I am brand new to php and any help will be appreciated.
I want to display an error message per input if someone attempts to submit the form and a field is empty, or if the file type selected is not the correct type or size. In the code below I have a unique message displayed, but on second thought I could just say "required" if there is some way to make this code better by doing that.
I tried using jQuery validate plugin and couldn't get it to work with the file upload (it is beyond my understanding of jQuery/Ajax/PHP), so this is the solution I came up with.
<?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
}
}
?>
Just giving you an idea for few fields for other fields try it by your self
//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>
Its just an idea to show you same logic while validating for file upload or have to use regex test for phone , email etc
Hope it makes sense
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.