1

My goal is to automatically upload an image to a folder when the file is selected. Following the answer on how do I auto-submit an upload form when a file is selected, I attempted to use Javascript's onchange event to automatically submit the form:

<?php
if(isset($_POST['upload']))
{
$ImageName = $_FILES['photo']['name'];
$fileElementName = 'photo';
$path = '../images/'; 
$location = $path . $_FILES['photo']['name']; 
move_uploaded_file($_FILES['photo']['tmp_name'], $location); 
}
?>

<form method="post" enctype="multipart/form-data">
<input type="file" name="photo" onchange="document.getElementById('upload').submit();" id="file" class="inputfile" />
<label for="file">Add Image</label>
<input type="submit" style="display: none;" name="upload" id="upload">
<input type="text">
<input type="submit">
</form>

When the file is selected, it's not automatically uploaded to a folder.

Note: I cannot use onchange="form.submit()" as I have multiple submit buttons in my form!

Community
  • 1
  • 1
The Codesee
  • 3,714
  • 5
  • 38
  • 78

1 Answers1

0

I think you could simply change it to:

document.getElementById('upload').click();

Or do something like this:

var button = document.getElementById('upload');
button.form.submit();
mister martin
  • 6,197
  • 4
  • 30
  • 63