3
<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

4

5 回答 5

5

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 
于 2013-09-25T07:05:54.210 回答
3

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/

于 2013-09-25T07:01:37.867 回答
0

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

于 2013-09-25T07:01:10.780 回答
0

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);
    });
});
于 2013-09-25T07:16:56.107 回答
0

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>
于 2013-09-25T07:20:10.697 回答