我有一个像这样的 contenteditable div:
<form>
<div name="new_post" class="post" contenteditable="true"></div>
<button type="submit">Submit</button>
</form>
contenteditable div 允许使用bold 和italic 标签,但不允许其他标签。
我的问题是,如果用户键入类似 Hello there <u>world</u>!
的内容, 它将在数据库中保存为 Hello there
.它似乎删除了标签后的所有内容,我不知道为什么。
我正在使用 AJAX 和 PHP 来处理帖子,所以这是其余代码。
Ajax :
$(document).ready(function() {
$(document).on("submit", "form", function(event) {
event.preventDefault();
alert($('.post').html()); // added this to debug it. This prints "Hello there <u>world</u>!"
$.ajax({
url: 'php/post.php',
type: 'POST',
dataType: 'json',
data: $(this).serialize() + "&post_field=" + $('.post').html(),
success: function(data) {
alert(data.message);
}
});
});
});
PHP:
<?php
$post = $_POST["post_field"];
// query to insert post into database goes here
$array = array('message' => $post);
echo json_encode($array);
?>
请帮忙!
最佳答案
改变:
$('.post').html()
到:
encodeURIComponent($('.post').html())
HTML 字符需要在 URL 参数中进行编码。
关于javascript - PHP 删除 HTML 标签后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892438/