0

EDIT I'm trying to send several input values trough jQuery / ajax() with a file in it and ajax() doesn't seems to support both in the same time. I've found that FormData would do the trick. Using jQuery ajax to upload file and form data with formData() http://www.thefourtheye.in/2013/10/file-upload-with-jquery-and-ajax.html

UPDATE I don't have any errors anymore but the file is not uploaded properly into the MySQL DB/LongBlob columb. It seems to be properly received now in the PHP file.

html

    <input type=hidden name=catselid id=catselid value=".$id.">
    <input name=city id=city>
    <input name=country id=country>
    <input type=file name=picture id=picture >
    <input name=update value=update type=submit class=update id=update  />

javascript

    $(".update").click(function(){

        $.ajax({
            url: 'catsel_change.php',
            type: 'POST',
            contentType:false,
            processData: false,
            data: function(){
                var data = new FormData();
                data.append('picture',$('#picture').get(0).files[0]);
                data.append('city' , $('#cityname').val());
                data.append('country', $('#country').val());
                data.append('id', $('#catselid').val());
                return data;
            }(),
                success: function(result) {
                alert(result);
                },
            error: function(xhr, result, errorThrown){
                alert('Request failed.');
            }
            });

    });

php

if(is_uploaded_file($_FILES['picture']['tmp_name'])){
    $picture =addslashes (file_get_contents($_FILES['picture']['tmp_name']));
...
}

FYI, I'm using jQuery 1.9.1 comments/suggestions are welcome regards

Community
  • 1
  • 1
poypoy
  • 107
  • 1
  • 13
  • 1
    Try looking at this answer: http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery :) – Radu Bompa Feb 12 '14 at 12:50
  • http://malsup.com/jquery/form/#ajaxSubmit an ajax/iframe method of sending a form with file. – Knobik Feb 12 '14 at 15:42

2 Answers2

0

Ok I got it finally. Just a stupid messy "," left behind in the php file. Now all works properly. Thanks for help!

poypoy
  • 107
  • 1
  • 13
-2

There is no way of sending file to the server through a true ajax call. i recommend you to put your form in a hidden iframe and submit it from the parent.

eyurdakul
  • 894
  • 2
  • 12
  • 29
  • 1
    Of course there _are_ ways, using the HTML5 File API – but you have to implement it yourself. – CBroe Feb 12 '14 at 13:22