views:

47

answers:

1

Ok, I hope this will be my last question in a series of Q's regarding dynamic file upload.

I'm using AjaxFileUpload Plugin and try to work with the FORM data in my uploader.php. The problem is that both $_POST and $_FILES is NULL.

This is my HTML code:

  <form id="uploadForm" enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    <input type="hidden" name="current_path" value="<?php echo $fb->relative_url; ?>" />
    <input id="uploadFile" name="uploadFile" type="file" />
    <input type="button" class="button uploadImage" value="<?php _e('Upload File') ?>" /> <br />
  </form> 

And this is my JS script:

  //File upload
    jQuery('.uploadImage').live('click',function() {
    ajaxFileUpload();
  });

  (...)

  function ajaxFileUpload() {
    jQuery.ajaxFileUpload ( {
        url:'../wp-content/plugins/wp-filebrowser/uploader.php', 
        secureuri:false,
        fileElementId:'uploadFile',
        dataType: 'json',
        success: function (data, status) {
            alert('Error: ' + data.error + ' - Respons: ' + data.respons)
        },
        error: function (data, status, e) {
            alert('Error: ' + e);
        }
      }
    )
    return false;   
  }

To test that I data is submited, I have the following PHP code:

  $data['error']    = $_POST['current_path'];  // Gives me NULL
  $data['respons']  = $_FILES['uploadFile']['name']; // Gives me NULL

  // Return result in json 
  echo json_encode($data);  

UPDATE

After very good help from Pekka (with his good set of eyes), I have got it working! The code is updated with the correct code.

+2  A: 

You are assigning

fileElementId:'uploadFile',

but your file field doesn't in fact have that ID.

And your PHP script should look in

$_FILES["uploadFile"]["name"]
Pekka
Well spotted! I added the ID but still get the same result :(
Steven
@STeven what does a `print_r($_POST)` yield?
Pekka
@Steven also, more importantly, shouldn't it be `$_FILES['uploadFile']['name'];`?
Pekka
I can't see any output from PHP because it's all in an Iframe (I think). I've tested oouputting the content in a IFrame I hadded in HTML code, and all works fine. It only breaks when I try to use the Ajax plugin. I've also addded link to mye files if you want to download and have a look.
Steven
@Steven see my last comment, that's more important I think
Pekka
Again well spottet. I've been looking at my code for so long that I have gotten "blind". Now everything works perfectly. Thanks you very much for your help. I've been stuck with this for three days and now I can finally move on. If I could award you more points, I would :)
Steven
Pekka, if u have time, could you take a look at a similar problem? http://stackoverflow.com/questions/3699837/ajaxfileupload-plugin-does-not-retrieve-post-data
Steven
@Steven already done.
Pekka