I'm trying to send files by uploading it through phpmailer. Already gone through a couple of documentation and solution but not able to find why it is throwing me this error "Undefined array key "attachment"......." and "Trying to access array offset on value of type null" I checked my payload using browser's network tab and it's clearly showing the attachment is present. Every other field is working perfectly as intended, receiving the mail with all the fields but not able get the attachment, the uploads folder is just there inside the current directory where the handler file is located. What am I doing wrong?
Here's my front end part:"
<form action="handler-att.php" method="post">
<!--the first and last names -->
<div class="row mb-4">
<div class="col-lg-6 col-sm-12">
<div class="form-outline">
<input id="first-name" class="form-control" required="" name="first_name" placeholder="First Name*"
type="text" />
<!-- <label class="form-label" for="first-name">First name</label> -->
</div>
</div>
<div class="col-lg-6 col-sm-12">
<div class="form-outline">
<input id="last-name" class="form-control" required="" name="last_name" placeholder="Last Name*"
type="text" />
<!-- <label class="form-label" for="last-name">Last name</label> -->
</div>
</div>
</div>
<!-- Email Mobile input -->
<div class="row mb-4">
<div class="col-lg-6 col-sm-12">
<div class="form-outline">
<input id="register-email" class="form-control" name="register_email" required
placeholder="Email Address*" type="email" />
<!-- <label class="form-label" for="register-email">Email</label> -->
</div>
</div>
<div class="col-lg-6 col-sm-12">
<div class="form-outline">
<input id="mobile-number" class="form-control" name="mobile_number" required
placeholder="Mobile Number*" type="tel" maxlength="10" pattern="^[6-9]\d{9}$"
title="Please enter a valid number" />
<!-- <label class="form-label" for="mobile-number">Phone</label> -->
</div>
</div>
</div>
<!-- Dropdown input -->
<div class="row mb-4">
<div class="col-lg-12 col-sm-12">
<div class="form-outline">
<select id="college-select" class="form-select" aria-label="Default select example" required
name="course_preference">
<option value="" selected disabled hidden="">Select the course you are interested in</option>
<option value="B.Ed">B.Ed</option>
<option value="M.Ed">M.Ed</option>
<option value="D.El.Ed">D.El.Ed</option>
<option value="GNM Nursing">GNM Nursing</option>
<option value="B.Sc Nursing">B.Sc Nursing</option>
<option value="Engineering">Engineering</option>
</select>
<!-- <label class="form-label" for="college-select">Select College</label> -->
</div>
</div>
</div>
<!-- ===File attachemnt==== -->
<input type="file" id="attachment" name="attachment">
<!-- Message input -->
<div class="form-outline mb-4">
<textarea class="form-control" id="description" name="description" rows="6" required></textarea>
<!-- <label class="form-label" for="description">Tell Us About Yourself</label> -->
</div>
<!-- Submit button -->
<button type="submit" class="btn-lg btn-primary btn-block mb-4 contact-btn ps-5 pe-5">Send Enquiry
<span class="bi-arrow-right"></span></button>
</form>
And here's the attachment part of my phpmailer code:
if($_FILES['attachment']['name']!=null){
if(move_uploaded_file($_FILES['attachment']['tmp_name'], "uploads/{$_FILES['attachment']['name']}"))
{
$mail->addAttachment("uploads/{$_FILES['attachment']['name']}");
}
}