Please have a look at this question I try to explain more and added the bounty: How to make the div editable to textarea and upload/delete existing images
UPDATED:
My question was too broad, so I updated it and asking only one part first (You can click on edit to see my question before if you'd like) I have a php code that has some html divs. I want to pass the value of those html divs (that are in the loop) to the javascript.
My code is basically getting the user details. E.g. user has entered some information such as name and description on the website and it gets stored in mysql and I fetch that information and show it on the website. Now I want to make those information be editable, so if user click on edit button that div changes into textarea and user should be able to edit that text and click on save button to save it (so it update that on mysql) and it shows the new information on website. And delete button can be use to delete it.
My code:
<?php
require "database.php";
session_start();
global $username;
$username = $_SESSION['userUsername'].$_SESSION['userId'];
function listFolderFiles($username){
$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) ) {
echo '<div class="grid">' . '<h2>' . $row["_name"] . '</h2>' . '</div>';
echo '<div class="grid2">' . '<p>' . $row["_desc"] . '</p>' . '</div>';
echo '<hr/>';
}
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($username);
<script>
function editName() {
console.log("calling");
var divName = $("grid").html();
console.log(divName);
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>
The error: it says the value is undefined of divName
. How can I get the value of _name
div that is inside my php to javascript?
Also, is there a better way to edit my div, so user can edit the information such as name and description same way as I explained above?