我已经在html中编写了一个表,并且如果用户点击编辑按钮,则使其行满足。 该表的数据来自mysql数据库。我希望我的用户能够更改内容可编辑字段,并在点击保存按钮后再次发送到mysql表。 首先,我想知道如何在字符串变量中保存内容可编辑的更改,以便我可以将它们发布到数据库。以下是我的相关代码:
<?php
$servername = "localhost";
$username = "hevak_neshat";
$password = "shir moz peste";
$dbname = "hevak_android_api";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM beacons";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<thead>
<tr>
<th>Major number</th>
<th>Minor number</th>
<th>Client</th>
<th>Location</th>
<th>Link to ad</th>
<th>Attachment</th>
<th>Edit</th>
</tr>
</thead>";
echo "<tbody>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["major"] . "</td><td>" . $row["minor"] . "</td><td>" . $row["client"] . "</td><td>" . $row["geolocation"] . "</td><td>" . $row["linktoadd"] . "</td><td>" . $row["attacment"] . "</td><td>";
echo "<button class=\"editbtn\">Edit</button>";
echo "<td><button class=\"savebtn\">Save</button></td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody></table>";
} else {
echo "no results";
}
?>
我的Javascript:
$(document).on("click", ".editbtn", function (event) {
event.preventDefault();
alert("click on items to edit");
var currentTD = $(this).parents('tr').find('td');
if ($(this).html() == 'Edit') {
$.each(currentTD, function () {
$(this).prop('contenteditable', true)
});
} else {
$.each(currentTD, function () {
$(this).prop('contenteditable', false)
});
}
});
更新: 在我的代码中,我使用了内容可编辑。但如果有人想知道将表中的用户更改移动到数据库,请告诉我,即使它是另一种方式。只是一个能够编辑内容并将内容移动到db的表。