每次上传新照片(不更新页面)时如何更改照片?

时间:2015-01-21 18:05:47

标签: javascript php html

每次在img-tag"照片",当我上传新照片(没有更新页面)时如何更改照片?我需要使用javascript。请帮帮我!

<img src="" width="400px" height="300px" name="photo">

<input type="file" name="image">

<?php
    if (isset($_POST['save'])){


        $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
        $image_name= addslashes($_FILES['image']['name']);
        $image_size= getimagesize($_FILES['image']['tmp_name']);


        move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);            
        $image_location="upload/" . $_FILES["image"]["name"];


        mysql_query("insert into employee (image_location)
            values ('$image_location) ") or die(mysql_error());

header('location: DataGrid.php');

    }

?>

1 个答案:

答案 0 :(得分:0)

使用Javascript,您可以使用以下代码更改Image src属性:

    <img src="" width="400px" height="300px" id="image_id" name="photo">

    <input type="file" name="image">

    <?php
        if (isset($_POST['save'])){


            $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
            $image_name= addslashes($_FILES['image']['name']);
            $image_size= getimagesize($_FILES['image']['tmp_name']);


            move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);            
            $image_location="upload/" . $_FILES["image"]["name"];


            mysql_query("insert into employee (image_location)
                values ('$image_location') ") or die(mysql_error());


        }

    ?>

    <script type="text/javascript">
         document.getElementById("image_id").src="<?= $image_location; ?>";
    </script>

但使用绝对图像位置可能更好。