0

I have this HTML:

<form action='uploadhandle.php' method='POST' enctype="multipart/form-data">
<input type='file' class='fileinput' id='photo1' name='photo1'>
<input type='button' id='upload1' name='upload1' value='Upload'>
</form>

My jquery code is:

$('#upload1').click(function(){
    $.ajax({
        url: "uploadhandle.php",
        data: 'photo1='+photo1,
        success: function(data){$('#result_div').html(data)}

        });

In my uploadhandle.php, when I try to display the $_POST['photo1'], nothing comes up, it's "undefined". Does anyone know what I did wrong?

Thanks a lot, Regards

2 Answers 2

6

You cannot upload a file via AJAX. It isn't possible.

What IS possible is using a plugin or another method that "simulates" ajax by creating an iframe and submitting the info in the background. There are several plugins that handle this, some are very complex, some simply extend the ajax function itself.

That being said, your syntax server side is also wrong. You have to deal with $_FILES not $_POST to find and use the submitted file.

Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your suggestions. I will look into that iframe method. Regards
0

You can't upload file data through XHR (i.e. ajax). There is a file upload API in XHR2, but the most practical and cross browser compatible way today seems to use a hidden iframe to do the file upload.

Edit: See How do you post to an iframe?

1 Comment

Thanks for clarifying, I am looking into your link. Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.