image preview before upload using PHP

When the user selects a picture, an onChange event is triggered on the file input field and that we can use JavaScript’s readURL() class to onchange event to display the image for preview. When the user choose the file, the input form is going to be submitted to an equivalent page. So on its same form.

Just modified your own code after copy below code

<style type="text/css">
        	margin: 10px 5px 0 0;
        	width: 100px;
            width: 120px;

<script type="text/javascript">
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {


  <form id="form1" action="server.php" enctype="multipart/form-data">
        <input type='file' name="file" onchange="readURL(this);" />
        <img id="blah" src="#" alt="your image" />
        <input type="submit" name="sub" value="submit">