简体   繁体   English

单击现有的个人资料图片以上传和更改图片。

[英]Clicking on existing profile picture to upload and change image.

I have made a user profile card where the user can click on choose file to select and upload an image.我制作了一个用户个人资料卡,用户可以在其中单击选择文件来选择和上传图像。 在此处输入图片说明

Everything works fine however I would like to change it so instead of having to click on "choose file" and then click on "upload", the user can just click on the existing image and be brought to the select image screen:一切正常,但是我想更改它,因此不必单击“选择文件”然后单击“上传”,用户只需单击现有图像即可进入选择图像屏幕: 在此处输入图片说明

and once they have selected a image and pressed open, the image is automatically changed?一旦他们选择了图像并按下打开,图像会自动更改?

This is my code to display the user profile card.这是我显示用户个人资料卡的代码。

  <?php
        //query to see if the user has uploaded a profile picture.
        $sql = "SELECT * FROM user_image WHERE userid = ?";
        $stmt = mysqli_prepare($connect, $sql);
        mysqli_stmt_bind_param($stmt,"i",$_SESSION['userid']);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        $userImage = mysqli_fetch_assoc($result);
        mysqli_stmt_close($stmt);
    ?>   


  <h1>User's Profile</h1>
  <?php if($userImage['is_set'] == 0){
            echo '<img src="profile_pics/default-profile-pic.png"/>'; 
   }
        else{
            echo '<img src="'.$userImage['image_dir'].'"/>';
   }
   ?>
   <form action="upload.php" method="POST" enctype="multipart/form-data">
                    <input type="file" name="file" accept="image/*">
                    <button type="submit" name="submit">Upload</button>
   </form>

Use a <label> (with for targeting the id on the file field):使用<label>for定位文件字段上的id ):

<label for="fileField"><img src="..."></label>

<input type="file" id="fileField" name="file" accept="image/*">

You can use a <label> tag attached the file input to trigger the open box or you can use a click handler on the image that triggers a click on the file input.您可以使用附加在文件输入上的<label>标签来触发打开框,或者您可以使用图像上的点击处理程序来触发对文件输入的点击。

document.querySelector('img').addEventListener('click', function(){
    document.querySelector('h1 + img').click();
});

To get the file to upload immediately when selected you have to attach a change event handler for the image input.要在选择后立即上传文件,您必须为图像输入附加更改事件处理程序。 Then submit the form in it.然后提交其中的表格。 You would have to change the submit button name to something other than submit to get this to work.您必须将提交按钮名称更改为提交以外的名称才能使其正常工作。

document.querySelector('input[type=file]').addEventListener('change', function(){
    this.form.submit();
});
<button type="submit" name="upload">Upload</button>
<label for="fileField"><img src="..." height="100px" width="100px"></label>

<input type="file" id="fileField" name="file" accept="image/*" hidden="true">

Using hidden = "true" , you can invisible the choose file.使用hidden = "true" ,您可以隐藏选择文件。

Using height and width inside the img tag, you can adjust the image size.使用 img 标签内的高度宽度,您可以调整图像大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM