Search code examples
phpjqueryjquery-uijquery-file-upload

jQuery File Upload, get and edit Data which is saved in DB


Edited

With the jQuery File Upload Documentation I am able to save Data with the pictures in MySQL DB (see pic 1+2)

Now where I'm stuck is how to retrieve the Data back. After the Upload I would like to show the pics with the Data from DB but there is nothing I can find how to do that.

If anyone knows how to edit the json appending the DB data OR just how I can var_dump it just to push me in the right direction that would be awesome! I can not find where the json gets created. The json now looks like this:

{"files":[{
    "name"         : "02 (1).jpg",
    "size"         : 12508,
    "type"         : "image\/jpeg",
    "url"          : "http:\/\/localhost:8888\/server\/php\/files\/02%20%281%29.jpg",
    "thumbnailUrl" : "http:\/\/localhost:8888\/server\/php\/files\/thumbnail\/02%20%281%29.jpg",
    "deleteUrl"    : "http:\/\/localhost:8888\/server\/php\/?file=02%20%281%29.jpg",
    "deleteType"   : "DELETE"
}]}

and I would like to make it like:

{"files":[{
    "name"         : "02 (1).jpg",
    "size"         : 12508,
    "type"         : "image\/jpeg",
    "url"          : "http:\/\/localhost:8888\/server\/php\/files\/02%20%281%29.jpg",
    "thumbnailUrl" : "http:\/\/localhost:8888\/server\/php\/files\/thumbnail\/02%20%281%29.jpg",
    "deleteUrl"    : "http:\/\/localhost:8888\/server\/php\/?file=02%20%281%29.jpg",
    "deleteType"   : "DELETE",
    "titleDB"      : "Title1",
    "textDB"       : "Lorem ipsum dolor...."
}]}

I tried (like rAjA explained) and changed the following

require('UploadHandler.php');
$upload_handler = new UploadHandler();
$response_enc = $this->upload_handler->initialize();

But than I get an error saying "JSON.parse: unexpected non-whitespace character after JSON data" and I googeld for that and I found information but there is nothing which helped me.

Can anyone help me on this one or knows where I can find the Information? Thank you!

picpic2


Solution

  • This will give you the basic idea of editing the json response from uploadhandler library,

    EDITED:

    In your uploadhandler.php library and change this line so that it will return the response,

    public function post($print_response = true) to public function post($print_response = false)

    public function get($print_response = true) to public function get($print_response = false)

    protected function initialize() to public function initialize()

    EDITED: change this too function __construct($options = null, $initialize = false, $error_messages = null)

    and in

    function initialize()
    //
                case 'POST':
                    return $this->post(); //Change this line like here
                    break;
    ////
                case 'GET':
                    return $this->get(); //Change this line like here
                    break;
    //
    

    Then in the function where you calling the library get the response back,

    require('UploadHandler.php');
    $upload_handler = new UploadHandler();
    $response = $upload_handler>initialize();
    
    print_r($response); //Dump the response here and check    
    
    $custom_arr = //Save all your custom variables here (DB insert id, Text etc)
    //Sample format of custom_arr
    //$custom_arr['insert_id']  = $mysql_primary_key_id;
    //$custom_arr['txt']  = $user_custom_text_field;
    //
    
    $response['custom_data'] = $custom_arr;
    
    echo json_encode($response);
    

    In your front end you can use the fileuploaddone callback to get the data and play with it.