I'm currently working on a website which mail user attachments to the owner of the website. However I used a form to submit image files to mail PHP file and then it send the email. It worked perfectly until I override form submit function with Javascript and use ajax to submit it and get the output from mail PHP file to add some additional functionalities.
<form id="contactForm" action="php/sendcontactusmail.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<!-- Additional Codes for email input and subject inputs -->
<div class="form-group">
<label for="subject" id="uploadImageLabelId">Upload Pictures of the Job</label>
<form enctype="multipart/form-data" >
<input type="file" name="fileToUpload" id="imageUploadInputId" accept="image/*" id="fileToUpload" multiple>
</form>
</div>
<!-- Additional Codes for Message inputs and others -->
<div class="col-md-12">
<button type="submit"class="btn btn-primary pull-right" id="btnContactUs">Send Message</button>
</div>
</div>
</div>
</form>
Javascript code:
$("#contactForm").submit(function(){
$.ajax({
type:"POST",
url : "php/sendcontactusmail.php",
dataType:"json",
data: $("#contactForm").serialize(),
success: function (data)
{
alert(data);
}
});
return false;
});
**My Problem **
PHP code:
if(filter_var($_POST["subject"], FILTER_SANITIZE_STRING)=="estimate")
{
if($_POST && isset($_FILES['fileToUpload']))
{
// Some Codes to send email
}
else
echo json_encode(isset($_FILES['fileToUpload']));
}
else
{
// Some codes for send separate email.
}
when I run my program, alert box shows false. Which clearly means that isset($_FILES['fileToUpload']) function returns false. I tried every way to fix that. But couldn't find the problem. Any guesses?
Note
When I run the website on my local server I selected a compatible jpg file. But it says file is not set.
Update
I went through lots of stackoverflow questions and none of their solutions gave me a true return statement. Hope this wouldn't be a duplicated question.