views:

497

answers:

1

hi,

I am using the following mootools1.2 form check js. http://mootools.floor.ch/en/demos/formcheck/

For form check is working fine, its working fine with ajax form post.

My problem is that when i post the form with AJAX with file upload.

Then its not returning me the $_FILES array to PHP side.

If i post the form normally (i.e. without ajax) then every thing working fine means all post and file array are coming to php side.

While ajax form post its returning the selected file name in the post array but not the full $_FILES arrat

A: 
<?php
    if(count($_FILES) > 0)
    {
     echo('<pre>');

     var_dump($_FILES);

     die('</pre>');
    }
?>

<html>
    <head>
     <title>Simple file upload with iframe</title>
    </head>
    <body>
     <form enctype='multipart/form-data' method='post' action='<?php echo($_SERVER['PHP_SELF']); ?>' target='iframe'>
      <input name='file' type='file' />
      <input type='submit' value='Send' />
     </form>
     <iframe name='iframe' style='width: 800px; height: 400px;'></iframe>
    </body>
</html>

As far as I know you cannot upload a file with AJAX, simply because you cannot access the file. The browsers have protected the content of an input type=file, for example IE7 will give C:\fakepath\filename.ext and Firefox will only give filename.ext. But these are strings and not the file itself. So you really need to submit the file using a form submit.

The code above will post the form content to an iframe, which is a commonly used workaround for the problem you have and prevents the page from reloading.

Wessel Rossing