PHP删除HTML标签后的所有内容

时间:2014-01-02 21:41:01

标签: javascript php ajax jquery

我有一个像这样的满足的div:

<form>
    <div name="new_post" class="post" contenteditable="true"></div>
    <button type="submit">Submit</button>
</form>

contenteditable div允许粗体斜体标签,但 没有其他标签

我的问题是,如果用户输入类似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 &lt;u&gt;world&lt;/u&gt;!"
        $.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);
?>

请帮忙!

1 个答案:

答案 0 :(得分:6)

变化:

$('.post').html()

为:

encodeURIComponent($('.post').html())

HTML字符需要在URL参数中编码。

相关问题