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 uploadersGntem– Gntem2012-09-20 05:13:42 +00:00Commented Sep 20, 2012 at 5:13
-
1It is a good idea to learn how php worksIbu– Ibu2012-09-20 05:14:20 +00:00Commented Sep 20, 2012 at 5:14
-
can i upload the files using Ajaxuser1275375– user12753752012-09-20 05:17:39 +00:00Commented Sep 20, 2012 at 5:17
Add a comment
|
2 Answers
This should answer your question:
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;
}
...
?>
Comments
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