PHP_USER1
PHP_USER1

Reputation: 628

uploading a pic from computer with single button in php

<html>
   <head>
      <title>File Upload Form</title>
   </head>
   <body>
      This form allows you to upload a file to the server.<br />
      <form action="getfile.php" method="post"><br />
         Type (or select) Filename: 
         <input type="file" name="uploadFile" />
         <input type="submit" value="Upload File" />
      </form>
   </body>
</html>

I am working on a php language . I am uploading a pic from my computer . But there are two buttons ie choose file and Upload file . CHOOSE FILE is used to select the picture from PC and upload file button is used as SUBMIT button but i need a single button by clicking on it it will open my pc window to browse the photos and select it and it also act as a submit button . Is there any way to do this with single button

Upvotes: 3

Views: 5486

Answers (5)

RyanB
RyanB

Reputation: 1297

This is what I've done in my project:

HTML:

<input type="button" id="btnUpload" value="Upload File" />
<form action="getfile.php" method="post" id="upload_form" style="display: none;">
    Type (or select) Filename: 
    <input type="file" name="uploadFile" />
</form>

Javascript

$(function(){
    $('#btnUpload').click(function(){
        //Show select file dialog
        $('#uploadFile').click();

        //Wait for user to select a file
        var tmr = setInterval(function(){
            if($('#uploadFile').val() !== "") {
                clearInterval(tmr);
                $('#upload_form').submit();
            }
        }, 500);
    });
});

Upvotes: 0

Jabran Saeed
Jabran Saeed

Reputation: 6158

Here's the code. Just use this as it is and it will work.

<html>
<head>
<title>File Upload Form</title>
</head>
<script>
$(document).ready(function(){
$("input[type='file']").on("change", function(){
$("#form1").submit();
});
});
</script>
   <body>
  This form allows you to upload a file to the server.<br />
  <form action="getfile.php" method="post" enctype="multipart/form-data" id="form1"><br />
     Type (or select) Filename: 
     <input type="file" name="uploadFile" />
     <input type="submit" value="Upload File" />
  </form>
  </body>
</html>

Upvotes: 0

Donovan Charpin
Donovan Charpin

Reputation: 3397

You can wait an event change on the input type="file" and send file after this event.

If you use jQuery, you can send the form with .submit().

Try with something like this

$("input[type='file']").on("change", function(){
   $("form").submit();
});

.on("change", handler) : http://api.jquery.com/change/

.submit() : http://api.jquery.com/submit/

Upvotes: 3

Mayank Sharma
Mayank Sharma

Reputation: 829

you need to change to things in your form tag:

1) Add a name tag: say name="frm"
2) enctype="multipart/form-data"

Then in your input file type add a function onchange="frm.submit();" // Where frm is name of the form 

Upvotes: 5

Shakti Patel
Shakti Patel

Reputation: 3862

used jquery uplodify: uploadify

Demo

$(function() {
    $("#file_upload_1").uploadify({
        height        : 30,
        swf           : '/uploadify/uploadify.swf',
        uploader      : '/uploadify/uploadify.php',
        width         : 120
    });
});

also check this for auto upload auto upload

Upvotes: 0

Related Questions