javascript - Upload img without using submit button or rederict in PHP & AJAX -


this might duplicate have not found matches needs yet. want upload image folder choosing file in file browser, no submit button used (so far good, can this). want user not redirected upload.php file. need submit file without using submit button , not redirecting user page. refresh ok preferable without refresh well.. have right now, , question if done in php,ajax & javascript or if should go jquery plugin?

and have several forms not using id-tag

my code far doesn't work 100%, when upload , redirect upload.php :

<form action="upload.php" method="post" enctype="multipart/form-data">   <input type="file" name="filetoupload" class="test"> </form>      $(".test").change(function() {          $.ajax({             url: "upload.php",             type: "post",             datatype: 'json',             processdata: false,             contenttype: false,             data('file', $(this[0].files[0])),             success: function(text) {                     alert("successfully");             }         });        }); 

upload.php

<?php if (move_uploaded_file($_files["filetoupload"]["tmp_name"], "uploads/myfile.jpg")) {     echo "the file ". basename( $_files["filetoupload"]["name"]). " has been uploaded.";  } else {     echo "sorry, there error uploading file.";      }  }?> 

you can use below method without button click , refresh page file upload.

<form id="uploadform" target="upiframe" action="upload.php" method="post" enctype="multipart/form-data">   <input type="file" name="filetoupload" class="test" onchange="yourfunction()"> </form>  <iframe id="upiframe" name="upiframe" witdh="0px" height="0px" border="0" style="width:0; height:0; border:none;"></iframe>   function yourfunction() {     var form = document.getelementbyid('uploadform');     form.submit();   }); } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -