Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. enter image description here

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: enter image description here

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>
like image 699
Zestyy99 Avatar asked Mar 12 '18 14:03

Zestyy99


People also ask

What does it mean when someone constantly changes their profile picture?

“People who keep on changing their profile pictures are insecure, lack in confidence and are often very flippant in their decisions. Such people are also found to be suspicious and don't trust others easily.


1 Answers

Use a <label> (with for targeting the id on the file field):

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

<input type="file" id="fileField" name="file" accept="image/*">
like image 138
ceejayoz Avatar answered Oct 13 '22 03:10

ceejayoz