Hanya Idrees
Hanya Idrees

Reputation: 451

Attachments added through JavaScript function are not sent

I am using the following loop to send/attach multiple attachments to my email message.

foreach(array_keys($_FILES['attachment']['name']) as $key) {
    $source = $_FILES['attachment']['tmp_name'][$key]; 
    $filename = $_FILES['attachment']['name'][$key]; // original filename from the client
    $mail->AddAttachment($source, $filename);
}

But only the first attachment i made is sended.

The form code is

<form method="POST" action="<?php $PHP_SELF ?>"  enctype="multipart/form-data">
<input type="file" name="attachment[]" id="attachment" size="30"
onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
<div id="moreUploads"></div>
<div id="moreUploadsLink" style="display:none;">
<a href="javascript:addFileInput();">Attach another File</a></div>
<input name="submit" type="submit" value="submit" />
</form>

When one click on Attach another file, another File Upload button is shown, through a JavaScript function:

<script type="text/javascript">
var upload_number = 1;
var attachmentlimit = 5;
function addFileInput() {
    var d = document.createElement("div");
    var file = document.createElement("input");
    file.setAttribute("type", "file");
    file.setAttribute("name", "attachment"+upload_number);
    d.appendChild(file);
    document.getElementById("moreUploads").appendChild(d);
    upload_number++;
    if(upload_number == attachmentlimit) {
          document.getElementById('moreUploadsLink').style.display='none';
    }
}
</script>

Only the file attached through the first FileUpload Button is attached and sended, not the others. Help.

Upvotes: 0

Views: 297

Answers (1)

Travesty3
Travesty3

Reputation: 14469

The problem is that you are setting the name attribute of your new input element to attachment1, attachment2, etc. You should be setting this name to attachment[], just like the original input you created.

If you still don't get all your attachments, you can try a var_dump($_FILES) in your PHP script to ensure that you are getting all the files the way you expect them.

Upvotes: 1

Related Questions