我在个人项目中使用 contenteditable 功能来更新 sql 数据库,但是当我更新内容时,它会将 html 标签添加到数据库中,即
<div id="lipsum" style="font-size: 11px; font-family: Arial, Helvetica, sans;
text-align: justify; font-style: normal; font-variant: normal; line-height: normal;">
<p style="font-size: 11px; line-height: 14px; margin-bottom: 14px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin tincidunt tincidunt tellus,
ac tincidunt magna imperdiet volutpat. Pellentesque pharetra lorem vitae velit gravida,
eget gravida tellus volutpat. Praesent viverra nulla at arcu fringilla, quis semper ligula
在剥离这些标签方面,我有什么解决方案?我可以使用 jquery 或 php 吗?谁能给我看一些工作示例?
这是我用来更新数据库的代码
保存.php
<?php
include("db.php");
$content = $_POST['content'];
$firstname = $_POST['firstname'];//get posted data
$content = mysql_real_escape_string($content);
$firstname = mysql_real_escape_string($firstname);//escape string
$sql = "UPDATE datadump SET firstname = '$firstname', content = '$content' WHERE id = '1'";
if (mysql_query($sql))
{
echo 1;
}
?>
js/js.js
$(document).ready(function() {
$("#save").click(function (e) {
var content = $('#content').html();
var firstname = $('#firstname').html();
$.ajax({
url: 'save.php',
type: 'POST',
data: {content: content, firstname: firstname},
success:function (data) {
if (data == '1')
{
$("#status")
.addClass("success")
.html("Data saved successfully")
.fadeIn('fast')
.delay(3000)
.fadeOut('slow');
}
if (data == '1')
{
$("#status")
.addClass("success")
.html("Data saved successfully")
.fadeIn('fast')
.delay(3000)
.fadeOut('slow');
}
else
{
$("#status")
.addClass("error")
.html("An error occured, the data could not be saved")
.fadeIn('fast')
.delay(3000)
.fadeOut('slow');
}
}
});
});
$("#maincontent").click(function (e) {
$("#save").show();
e.stopPropagation();
});
$(document).click(function() {
$("#save").hide();
});
});