i have a mail feature in wordpress admin plugin and i want the ability to attach a file dynamically and send it through mail as an attachment. i using ajax method but i did not know how to send input file through ajax This is html code
<div class="kids-top-right-view">
<ul class="kids-list kids-list-inline kids-nav-item">
<li>
<a>
<i class="fa fa-envelope-o"></i> Email</a>
</li>
</ul>
<div class="note_container">
<form method="post" id="mail_parent_form" enctype="multipart/form-data">
<input type="hidden" name="action" value="attach_file_parent">
<p class="email-from">
<label>From</label>
<span class="sep">:</span>
<span class="value">Casting Kids(office@castingkids.com.au)</span>
</p>
<p class="email-to">
<label>To</label>
<span class="sep">:</span>
<span class="value"><?=$child[0]->fname.' '.$child[0]->lname?></span>
<input type="hidden" class="email_address" value="<?=$child[0]->email?>">
</p>
<p class="email-subject">
<label>Subject</label>
<span class="sep">:</span>
<span class="value">
<input type="text" name="email_subject" placeholder="Subject...">
</span>
</p>
<div id="parent_mail">
<trix-editor placeholder="Type your email body ....."></trix-editor>
<div class="client-action">
<input type="submit" class="button button-primary" id="email_P" name="email_parent" value="Send Mail" >
<input type="file" name="file[]" id="email_file" multiple>
<label for="email_file">Attach File</label>
<input type="reset" class="button button-default" value="Discard">
<p style="display: none;" class="error_message"></p>
<p style="display: none;" class="success_message"></p>
</div>
</div>
</form>
</div>
</div>
Jquery Code
$("#email_file").on('change',function(){
var input = document.getElementById('email_file');
var files = input.files;
data = {'action':'attach_file_parent','files':files};
jQuery.post(ajaxurl, data, function(response) {
})
})
But it gives me this error: Illegal Invocation beacause i dont know where to add this line processData: false to tackle this error and i have to use this method for ajax. i cant be able to use this
$.ajax({
url : ajaxurl,
type : 'POST',
data : formData,
contentType: false,
processData: false
})
as it gives me ajax not found error Please suggest me with this, thank you
Please check the code below to upload form image through Ajax
JS CODE
You need to add File type filed with ID img_avatar
WordPress Code (PHP)