0

I have a registration form and a button when onclick should upload the files in server before the form is submitted. How could i run the PHPscript whenever the button is clicked to upload the files

user1275375
  • 1,361
  • 5
  • 23
  • 38
  • you cant do that, work on client-side to send a request to PHP using AJAX.. instead of doing that from scratch try searching for ajax file uploaders – Gntem Sep 20 '12 at 05:13
  • 1
    It is a good idea to learn [how php works](http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/) – Ibu Sep 20 '12 at 05:14
  • can i upload the files using Ajax – user1275375 Sep 20 '12 at 05:17

2 Answers2

1

This should answer your question:

Ajax File Upload with PHP

The HTML code:

<form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload.php">
<input name="file" id="file" size="27" type="file" /><br />
<input type="submit" name="action" value="Upload" /><br />
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>

The Javascript:

function init() {
    document.getElementById('file_upload_form').onsubmit=function() {
        document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
    }
}
window.onload=init;

The upload.php PHP code (Some code excluded for brevity):

<?php
...
if($_FILES['image']['name']) {
    list($file,$error) = upload('image','uploads/','jpeg,gif,png');
    if($error) print $error;
}
...
?>
Frankline
  • 40,277
  • 8
  • 44
  • 75
0

Its better you go on with Ajax rather trying to scratch your head for something that is impossible without Ajax. because you want to post a partial functionality and then the actual submit. you can start learning Basic Ajax or you can directly use any Ajax Framework.

Here are Some already build Ajax uploaders, you can check and use them with your application

ScoRpion
  • 11,364
  • 24
  • 66
  • 89