-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobapply.php
executable file
·277 lines (233 loc) · 9.94 KB
/
jobapply.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
include 'partials/errorreporting.php';
include 'partials/config.php';
if (isset($_GET['id'])) {
$jobID = $_GET['id'];
}
$user_id = mysqli_real_escape_string($conn, $_SESSION["id"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = $_POST['name'];
$email = $_POST['email'];
$contactNumber = $_POST['contact_number'];
$coverletter = $_POST['cover_letter'];
// Check if the user is the owner of the job listing
$isOwner = checkIfUserIsOwner($user_id, $jobID);
if ($isOwner) {
// Display an error message indicating that the user cannot apply for their own job
echo "<h1> You cannot apply for your own job.</h1>";
header("Refresh:1 ; URL=jobs.php");
} else {
if (isset($_FILES["resume"]) && $_FILES["resume"]["error"] == 0) {
$allowedTypes = ["application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"];
$maxFileSize = 1048576; // 1MB
$fileType = $_FILES["resume"]["type"];
$fileSize = $_FILES["resume"]["size"];
if (in_array($fileType, $allowedTypes)) {
if ($fileSize <= $maxFileSize) {
$uploadDir = "partials/images/";
$filename = uniqid("resume_", true) . "." . pathinfo($_FILES["resume"]["name"], PATHINFO_EXTENSION);
$destination = $uploadDir . $filename;
if (move_uploaded_file($_FILES["resume"]["tmp_name"], $destination)) {
// Prepare the INSERT statement
$stmt = mysqli_prepare($conn, "INSERT INTO applicants (user_id, job_id, applicant_name, contact_number, email, cover_letter, resume) VALUES (?, ?, ?, ?, ?, ?, ?)");
// Bind the parameters and execute the statement
mysqli_stmt_bind_param($stmt, "iisssss", $user_id, $jobID, $name, $contactNumber, $email, $coverletter, $destination);
if (mysqli_stmt_execute($stmt)) {
echo "Application submitted successfully!";
header("Refresh:1 ; URL=index.php");
} else {
echo "Error: Failed to submit the application.";
}
} else {
echo "Failed to upload the file.";
}
} else {
echo "File size exceeds the maximum limit (1MB).";
}
} else {
echo "Invalid file type. Only PDF, DOC, and DOCX files are allowed.";
}
} else {
echo "Error occurred during file upload.";
}
}
}
// Function to check if the user is the owner of the job listing
function checkIfUserIsOwner($user_id, $job_id) {
global $conn;
$query = "SELECT user_id FROM job_detail WHERE job_id = ?";
// Prepare and execute the query
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "i", $job_id);
mysqli_stmt_execute($stmt);
// Bind the result
mysqli_stmt_bind_result($stmt, $owner_id);
// Fetch the result
if (mysqli_stmt_fetch($stmt)) {
// Compare the owner's user ID with the current user's user ID
return $owner_id == $user_id;
}
// Close the statement
mysqli_stmt_close($stmt);
return false;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Job Application</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="applystyles.css">
</head>
<body>
<div class="container">
<h2>Job Application</h2>
<form enctype="multipart/form-data" id="applyForm" method="POST" action="">
<div class="form-group">
<label for="fullName">Full Name</label>
<input type="text" class="form-control" id="fullName" name="name" placeholder="Enter your full name" required>
<span id="fullName_err" class="error" style="color:red"></span>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email"
required>
<span id="email_err" class="error" style="color:red"></span>
</div>
<div class="form-group">
<label for="contactNumber">Contact Number</label>
<input type="tel" class="form-control" id="contactNumber" name="contact_number"
placeholder="Enter your contact number" required>
<span id="contactNumber_err" class="error" style="color:red"></span>
</div>
<div class="form-group">
<label for="resume">Resume</label>
<div class="custom-file">
<label class="custom-file-label" for="resume">Choose file</label>
<input type="file" class="custom-file-input" id="resume" name="resume" required>
<?php
if (isset($err['resume'])) {
echo $err['resume'];
}
?>
</div>
</div>
<script>
document.getElementById('resume').addEventListener('change', function(e) {
var fileName = e.target.files[0].name;
var label = document.querySelector('.custom-file-label');
label.innerText = fileName;
});
</script>
<div class="form-group">
<label for="coverLetter">Cover Letter</label>
<textarea class="form-control" id="coverLetter" name="cover_letter" rows="5"
placeholder="Enter your cover letter" required></textarea>
<span id="coverLetter_err" class="error" style="color:red"></span>
</div>
<br>
<center>
<button type="submit" id="submitButton" class="btn btn-primary" >Submit Application</button>
<br><br>
</center>
</form>
</div>
<!-- Bootstrap JS -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script type="module">
// Function to validate the Contact Number field
function validateContactNumber(number) {
var numberRegex = /^\d{10}$/;
return numberRegex.test(number);
}
// Function to validate the Email field
function validateEmail(email) {
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
// Function to validate the Company Name, Description, and Requirements fields
function validateTextWithNoNumber(text) {
var textRegex = /^[a-zA-Z][a-zA-Z0-9\s]*$/;
return textRegex.test(text);
}
// Function to check if all error fields are empty
function areErrorFieldsEmpty() {
return (
$("#fullName_err").text() === "" &&
$("#email_err").text() === "" &&
$("#contactNumber_err").text() === "" &&
$("#coverLetter_err").text() === ""
);
}
// Function to enable or disable the Submit Button based on error fields
function updateSubmitButtonState() {
var submitButton = $("#submitButton");
if (areErrorFieldsEmpty()) {
submitButton.removeAttr("disabled");
} else {
submitButton.attr("disabled", "disabled");
}
}
// Function to perform real-time validation for each field on key press
$(document).ready(function () {
// Company Name validation
$("#fullName").on("keyup", function () {
var fullName = $(this).val();
var errorSpan = $("#fullName_err");
if (validateTextWithNoNumber(fullName)) {
errorSpan.text("");
} else {
errorSpan.text("Full Name should not begin with a number.");
}
updateSubmitButtonState(); // Update Submit Button state
});
// Email validation
$("#email").on("keyup", function () {
var email = $(this).val();
var errorSpan = $("#email_err");
if (validateEmail(email)) {
errorSpan.text("");
} else {
errorSpan.text("Invalid Email.");
}
updateSubmitButtonState(); // Update Submit Button state
});
// Contact Number validation
$("#contactNumber").on("keyup", function () {
var contactNumber = $(this).val();
var errorSpan = $("#contactNumber_err");
if (validateContactNumber(contactNumber)) {
errorSpan.text("");
} else {
errorSpan.text("Invalid Contact Number. Please enter 10 digits.");
}
updateSubmitButtonState(); // Update Submit Button state
});
// Cover Letter validation
$("#coverLetter").on("keyup", function () {
var coverLetter = $(this).val();
var errorSpan = $("#coverLetter_err");
if (coverLetter.trim() !== "") {
errorSpan.text("");
} else {
errorSpan.text("Cover Letter is required.");
}
updateSubmitButtonState(); // Update Submit Button state
});
// Display the selected file name for the resume
$("#resume").on("change", function (e) {
var fileName = e.target.files[0].name;
var label = document.querySelector(".custom-file-label");
label.innerText = fileName;
});
// Initial state setup
updateSubmitButtonState();
});
</script>
</body>
</html>