![The image file name is 'default.jpg']](../../images/3800435809.webp)
I want that when the button for change picture is clicked an input type=file opens up and as soon as the image is selected the image src is updated. How can I achieve this? I am using PHP as the server side language and trying to do on web page.
![The image file name is 'default.jpg']](../../images/3800435809.webp)
I want that when the button for change picture is clicked an input type=file opens up and as soon as the image is selected the image src is updated. How can I achieve this? I am using PHP as the server side language and trying to do on web page.
You can doit using HTML5 FileReader. You don't need to use PHP or server side for that.
See this JSFidlle: https://jsfiddle.net/166p3uqk/1/
You don't need a FORM or value to your INPUT text.
HTML:
<input onchange=file_changed() type=file id=input>
<img id=img>
Javascript:
function file_changed(){
var selectedFile = document.getElementById('input').files[0];
var img = document.getElementById('img')
var reader = new FileReader();
reader.onload = function(){
img.src = this.result
}
reader.readAsDataURL(selectedFile);
}
More info:
Don't know if you need the image to be uploaded to server first or not But just for front-end it could be done by:
<input type="file" class="changeimage" id="changeimage" accept="image/*"/>
<img class="image" src="default.png" style="height:120px"/>
<script>
document.getElementById("changeimage").addEventListener("change", function(){
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById("image").setAttribute('src', e.target.result);
}
});
</script>
You can also find a solution at onchange file input change img src and change image color
Good Luck:)