The Codesee The Codesee - 6 months ago 20
HTML Question

Upload image immediately after selected

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!

Answer

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();
Comments