2

i have a problem with my php fileupload which im using via ajax in jquery.

This is my code in the php upload file:

if ($_FILES["file"]["error"] > 0)
{
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
    move_uploaded_file($_FILES["file"]["tmp_name"],"termine.csv");
    echo "Stored in: " . $_FILES["file"]["name"];
}

This is my jquery ajax statement:

 $('#upload_csv').click( function() {
    $.ajax({
        url: '/playground/php/updatedates.php',
        type: 'POST',
        data: $('form#csv').serialize(),
    }).done(function(txt) {
            if(txt!='')alert(txt);
    });
});

and this is my form in the index.php file:

<form enctype="multipart/form-data" id="csv" method="POST">
            <div style="float:left">
            <input name="file" type="file" id="file" size="100">
            <br/>
            <input type="submit" id="upload_csv" value="upload csv file">
            </div>
        </form>

The only error message i get is "error" and nothing else.

Hope somebody can help.

5
  • Please change js line like this data: $('#csv').serialize(), Commented Nov 9, 2012 at 10:41
  • @Elby There's no difference between $('#csv') and $('form#csv') if #csv is a form (which it is) Commented Nov 9, 2012 at 10:44
  • @iliketocodenstuff Please create a else part if(txt!='')alert(txt); else alert("HERE"); Let me know what is the result Commented Nov 9, 2012 at 10:47
  • @Elby the alert("here") isn't executed. Commented Nov 9, 2012 at 10:48
  • that means error in your updatedates.php or nothing to get in $_FILE so please comment all other codes in that echo any this and check should you get any any result in alert Commented Nov 9, 2012 at 10:51

1 Answer 1

2

$().serialize always creates a url-encoded string, and ignores file input. You have to use another approach. See: http://api.jquery.com/serialize/

One option is to put the form in a hidden iframe and trigger the submit event.

Also take a look at the jQuery File Upload plugin: http://blueimp.github.com/jQuery-File-Upload/

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

2 Comments

I made it without using ajax and redirect from the uploadfile.php back to the index. This is "uglier", but simple. Thank you for your help.
@iliketocodenstuff Have you looked at the plugin? It looks very easy to use and it uses the iframe method as a fallback internally.

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.