I am working on my jquery code to count on how many data I have and fetch the filename name from the PHP array. I have the return output for result.data.attachment
which it show [object Object]
.
$.ajax({
url: 'readDraft.php',
type: 'POST',
data : {
email_number: email_number
},
dataType: 'json',
success: function(result) {
alert(result.data.attachment);
}
});
Here is the full code for ajax function:
$(document).on('click','#openDraft', function(e) {
e.preventDefault();
var email_number = $(this).closest('tr').attr('data-id');
var encryptedAES = $(this).closest('tr').attr('data-url');
var id = Math.round((Math.random() * 36 ** 12)).toString(36);
if ($("#movetoFolder").css('display') == 'block') {
$('#movetoFolder').hide();
}
total_compose.push(id);
compose_count++;
for (var i = 0; i < total_compose.length; i++)
{
var popup_class = "popup-window-"+total_compose[i];
$('#'+popup_class).hide();
newMail("", id, compose_count);
}
var popup_class = "popup-window-"+total_compose[total_compose.length-2];
var popup_window = "popup-window-"+id;
email_receipt = $('#domodal_email_receipt').val();
email_subject = $('#domodal_subject').val();
email_message = $('#domodal_message').val();
$.ajax({
url: 'readDraft.php',
type: 'POST',
data : {
email_number: email_number
},
dataType: 'json',
success: function(result) {
alert(result);
if (result.data.receipt != '') {
$("#domodal_email_receipt").val(result.data.receipt);
}
if (result.data.subject != '') {
$("#domodal_subject").val(result.data.subject);
}
if (result.data.message != '') {
$("#domodal_message").text(result.data.message);
}
if (result.data.attachment > 0) {
var fileName = result.data.attachment['filename'];
alert("here!");
alert(result.data.attachment);
alert(result.data.attachment['filename']);
//var add_attachment = '<div class="dL" tabindex="-1" id="vw" aria-label="Attachment: '+fileName+'. Press enter to view the attachment and delete to remove it" style="position:relative;"><div style="display:flex"><a class="dO" id="attach_url'+attach_url_id+ '" href="http://test.example.com/u/?id=&attid=0&msg=6685135251679589431&view=att&disp=safe" target="_blank" style="display:flex;"><div id="domodal_attachment" class="vI">'+fileName+'</div><div class="vJ">'+fileSize+'</div></a><div id="vn" role="button" aria-label="Remove attachment" class="vq" tabindex="-1" data-tooltip="Remove attachment" style="position:absolute; right:5px; top:5px;"></div></div></div>';
//$('.file-list_yhs').append(add_attachment);
}
}
});
window.history.pushState("object or string", "Title", "http://test.example.com/#draft/"+encryptedAES);
$('#'+popup_class).show();
});
Here is the readDraft.php:
<?php
$structure = imap_fetchstructure($draft, $email_number);
$overview = imap_fetch_overview($draft, $email_number);
$draft_from = $overview[0]->from;
$draft_to = $overview[0]->to;
$draft_subject = utf8_decode(imap_utf8($overview[0]->subject));
$draft_fulldate = $overview[0]->date;
$draft_date = convDate($draft_fulldate);
$draft_message = quoted_printable_decode(imap_fetchbody($draft, $email_number, 1.2));
$attachments = array();
if(isset($structure->parts) && count($structure->parts)) {
for($i = 0; $i < count($structure->parts); $i++) {
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i+1);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
$data = array("receipt"=>$draft_to, "subject"=>$draft_subject, "encryption"=>$email_number_encrypt, "message"=>$draft_message, "attachment"=>$attachments);
$response = array("data"=>$data, "success"=>"successfully", "total_inbox"=>$total_inbox_unread, "total_spam"=>$total_spam_unread);
echo json_encode($response);
?>
Output for readDraft.php attachment:
Array
(
[1] => Array
(
[is_attachment] => 1
[filename] => draft_attachment.rar
[name] => draft_attachment.rar
[attachment] =>
)
)
Array
(
[2] => Array
(
[is_attachment] => 1
[filename] => draft_attachment - Copy.rar
[name] => draft_attachment - Copy.rar
[attachment] =>
)
)
I have tried this:
success: function(result) {
if (result.data.attachment.length > 0) {
...do something
}
}
It wont let me to get pass on the if statement. I have also tried this which it didn't work:
if (result.data.attachment > 0) {
...do something
}
What I am trying to do is I want to check on result.data.attachment
that if I have one or more attachment then I want to fetch the attachments filename.
Can you please show me an example how I could be able to check if I have one attachment or more and how I can also fetch the filename from the attachments array?
Thank you.