简体   繁体   English

如何使 div 可编辑到 textarea 并上传/删除现有图像

[英]How to make the div editable to textarea and upload/delete existing images

I have a website where user can upload images, name and description.我有一个网站,用户可以在其中上传图片、名称和描述。 I am saving it in mysql and fetching that information to show on my website.我将它保存在 mysql 并获取该信息以显示在我的网站上。 That's what my below code does, let the user enter those information (image and text) and show it on the website when they click submit button.这就是我下面的代码所做的,让用户输入这些信息(图像和文本)并在他们单击提交按钮时将其显示在网站上。

My question is how can I make the div (that shows images, name and desc) to editable when user clicks the edit button (so change that same div to textarea or something that is editable) and for images should have an cross mark near the image when clicked on edit button, so user can delete it and upload button to upload more images.我的问题是,当用户单击编辑按钮(因此将相同的 div 更改为 textarea 或可编辑的内容)时,如何使 div(显示图像、名称和 desc)可编辑,并且图像应该在附近有一个十字标记单击编辑按钮时的图像,因此用户可以将其删除并上传按钮以上传更多图像。

What I have done: I try to use javascript to get the value of div and use that to change it to textarea, but it says the value is undefined for the grid div.我所做的:我尝试使用 javascript 来获取 div 的值并使用它来将其更改为 textarea,但它表示grid div 的值未定义。

So, how can I make my div editable as explained above, as what I have try so far is not complete, so is there any better way to make the div editable same way I explained above so user can edit text and upload or delete images?那么,我怎样才能使我的 div 像上面解释的那样可编辑,因为到目前为止我所尝试的还没有完成,所以有没有更好的方法让 div 像我上面解释的那样可编辑,这样用户就可以编辑文本并上传或删除图像?

My code:我的代码:

 <?php
    
    require "database.php"; // connecting to database
    session_start();
    
    global $username;
    $username = $_SESSION['userUsername'].$_SESSION['userId']; //fetching the username
  
    
    $image = "userPos/$username"; //fetching image posted by specific user
    
    function listFolderFiles($dir, $username){
    
   <!-- The div my_class is getting the images from local storage
         that was initially uploaded by user in the website
 (and we stored it in the local storage) to display them on the website -->

    echo '<div class="my_class">';  
        $ffs = scandir($dir);
    
        unset($ffs[array_search('.', $ffs, true)]);
        unset($ffs[array_search('..', $ffs, true)]);
        // prevent empty ordered elements
        if (count($ffs) < 1)
            return;
   
        foreach($ffs as $ff){
      
        $s = "'<li>'.$ff";
         $saving = "$dir/$ff";
          $string = "$saving";
          global $string_arr;
          $string_arr = (explode("/",$string));
          $sav;
          $sav =  '<li>'.$ff;
          global $sa;
          $sa = "$ff";
          
          echo '<div class="imagess">';
    
       
           if(is_file($saving)) {
            
            echo '
     <div class="images">
          <img src="'.$saving.' " width="100" height="100" alt="Random image" class="img-responsive"  />
          </div>
            
            ' ;
           }
           echo `</div>`;
    
        
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff, $username);
        
        }
        echo `</div>`;
       
    
     
        require "database.php";
    
        <!--Here we are fetching the name and description
         that was entered by user on the website
          and displaying them on the website now-->

        $username = $_SESSION['userUsername'].$_SESSION['userId'];
        
        $sql = "SELECT * FROM `_desc` WHERE `username` = '$username' AND `id` = '$string_arr[2]';";
        
        $result = mysqli_query($conn, $sql);
        $resultCheck = mysqli_num_rows($result);
        
     
        if($resultCheck > 0) {
            echo '</br>';
        
            while($row = mysqli_fetch_assoc($result) ) {
               //name and desc
                echo '<div class="grid">' . '<h2>' .  $row["_name"] . '</h2>' . '</div>';
               
                echo '<div class="grid2">' . '<p>' .  $row["_desc"] . '</p>' . '</div>';
          
              
            }
    
    <!--Here I am trying when user click on edit button it should
     change that div to editable textarea so user can edit
      and save it or delete the whole div-->
   
     echo '<button onClick="editName()" class="btn btn-info"> 
      Edit</button>';
        echo '<a href="deleteUserInfo.php?edit= '. 
       $row["id"].'"class="btn btn-danger ">Delete</a>';
    
           
        }
        echo '</div>';
    }
    listFolderFiles($image, $username);
    
   
    ?>
      <script>



function editName() {
    console.log("calling");
    var divName = $("grid").html(); 
    console.log(divName); //value is undefined here
    var editableName = $("<textarea />");
    editableName.val(divName);
    $("grid").replaceWith(editableName);
    editableName.focus();
    editableName.blur(saveName);

}

function saveName() {
    var htmlName = $(editableName).html();
    var viewableText = $("<div>");
    viewableText.html(htmlName);
    $(editableName).replaceWith(viewableText);

    $(viewableText).click(editName);
}

</script>  
    
    
    </body>
    </html>

When the user clicks the edit button, set the contenteditable attribute to true on the target element and set the focus to that element.当用户单击编辑按钮时,将目标元素的contenteditable属性设置为 true 并将焦点设置到该元素。

function editName() {
    $("grid").attr("contenteditable", true);
    $("grid").focus();
    $("grid").blur(saveName);
}

It is quite simple, you should use contenteditable attribute available and set that value to true.这很简单,您应该使用可用的contenteditable属性并将该值设置为 true。 This will make the div element editable.这将使 div 元素可编辑。

Like this,像这样,

 <div class="grid" contenteditable="true">I am editable!</div>

In your case, you should use a function to select the element and set the attribute to true.在您的情况下,您应该使用 function 到 select 元素并将属性设置为 true。 And for the div element on clicking the type='file' input tag, you can write function that will delete the file that is previously uploaded and upload the new file.对于单击type='file'输入标签的 div 元素,您可以编写 function ,这将删除之前上传的文件并上传新文件。 I would recommend you to research before posting a question in the community as you might get negative impacts on such questions.我建议您在社区中发布问题之前进行研究,因为您可能会对此类问题产生负面影响。 Hope it helps!希望能帮助到你! Happy coding!!编码快乐!!

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

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