【发布时间】:2014-01-02 21:41:01
【问题描述】:
我有一个这样的 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 来处理帖子,所以这是其余的代码。
阿贾克斯:
$(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);
?>
请帮忙!
【问题讨论】:
-
去掉不需要的标签的代码在哪里?
-
这很奇怪。没有,但它正在删除它。
-
可能是你的网络服务器模块(比如 apache 的 mod-security)
-
在连接到序列化数据之前是否需要对 HTML 进行 URL 编码?
-
为什么人们从来不让 jQuery 做序列化?试图超越 jQuery 永远行不通
标签: javascript php ajax jquery