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

3
  • 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 Commented Sep 20, 2012 at 5:13
  • 1
    It is a good idea to learn how php works Commented Sep 20, 2012 at 5:14
  • can i upload the files using Ajax Commented Sep 20, 2012 at 5:17

2 Answers 2

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;
}
...
?>
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.