1

This is the main part of the JavaScript file.

$(document).ready(function()
{
    $('form').submit(function(event)
    {
        var formData = {
            'name'          : $('input[name=name]').val(),
            'position'      : $('input[name=position]').val(),
            'resume'        : $('input[name=resume]').val(),
            'comment'       : $('#commentBox').val()
        };

        $.ajax({
            type        : 'POST',
            url         : 'process.php',
            data        : formData,
            dataType    : 'json',
            encode      : true
        })

It gets HTML form fields and posts them to the PHP file.

I then access all the fields via $_POST['..']

Problem is the 'resume' field only passes on a String like : "C:/fakepath/file.doc" but I need to pass on the actual object/file so I could access it with $_FILES['resume']['name'] in the PHP file.

I'm sure there is something simple I'm missing but I've stared at it far too long and Googled examples without much success.

Appreciate any assistance, criticism :)

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 3
    ajax can't send files mate – Velimir Tchatchevsky Sep 20 '16 at 08:17
  • You can use plugins though ex:https://blueimp.github.io/jQuery-File-Upload/ – ka_lin Sep 20 '16 at 08:18
  • Sending file using AJAX is possible but need some tuning: http://stackoverflow.com/questions/9622901/how-to-upload-a-file-using-jquery-ajax-and-formdata – John Sep 20 '16 at 08:21
  • Just a follow up on this, I realized it was totally irrelevant as I was using the PHPMailer class, which provides you with a very easy method to add attachments to the emails like so: `$mail->addAttachment($fileName,$target_path); // Optional name` – user6852183 Oct 05 '16 at 03:12

1 Answers1

0

Try this as a form data object instead - you cannot send files - but can pass its data as a file.

var resume = $('#resume');   
var form_data = new FormData();                  
form_data.append('file', resume);

...etc

Caspar Wylie
  • 2,818
  • 3
  • 18
  • 32