简体   繁体   中英

using isset($_POST['upload']){ … } and calling it on form input type file change and not the submit button

In my php page, I have a form as:

   <form enctype = 'multipart/form-data' method="post">

        Select File:
        <br/>
        <input type="file" name="file" /> 
        <input type="submit" name="upload" value="Upload" />

    </form>

When I submit the form on clicking the submit 'upload', the following php is called which is on the same php page:

<?php
       if (isset($_POST['upload'])) {  ...Do stuff... } 
?> 

Now, I dont want to use the submit in my html form and want to trigger this php whenever a file in the input type file is selected. ie the form should be submitted automatically on file selection without the submit button. Also, i want a if condition such as isset($_POST['']) and dont want to use just a post check as $_SERVER['REQUEST_METHOD'] == 'POST'

Any help will be appreciated.

All you have to set an event handle for change in input file

<script type="text/javascript">
$(function() {
 $("input:file").change(function (){
   $('form').submit();
 });
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM