发布短信停止&

时间:2014-01-02 17:59:28

标签: php jquery

我不知道我的代码有什么问题但是当我尝试在任何行或单词上发布带有&的邮件时,它会在该点停止并仅发布帖子邮件文本之前&

例如我正在使用此帖子消息:Lorem Ipsum dolor & set imet如果我发布此消息,则仅发布此Lorem Ipsum dolor而不是整个句子。

我的jQuery是:

jQuery(document).ready(function($)
{
    $("#post_maroozat").on('click', function()
    {
        var message = $( "#maroozat_message" ).val();

        var posthash = $( "#posthash" ).val();
        var lastpid = $( "#lastpid" ).val();
        var tid = $( "#tid" ).val();
        var fid = $( "#fid" ).val();

        if (message == '')
        {
            alert("Message is missing!!");
            return;
        }
        $.ajax(
        {
            type : "post",
            dataType: "html",
            url : "misc.php?action=post_maroozat",
            data : "message="+message+"&posthash="+posthash+"&lastpid="+lastpid+"&tid="+tid+"&fid="+fid,
            success : function(response)
            {
                $('#show_maroozat').html(response);
                $('#maroozat_message').val('');
            }
        });
        return false;
    });
});

PHP:

if ($mybb->input['action'] == "post_maroozat")
{
    // Set up posthandler.
    require_once MYBB_ROOT."inc/datahandlers/post.php";
    $posthandler = new PostDataHandler("insert");

    // Set the post data that came from the input to the $post array.
    $post = array(
        "tid" => $mybb->input['tid'],
        "fid" => $mybb->input['fid'],
        "uid" => $mybb->user['uid'],
        "username" => $mybb->user['username'],
        "message" => $mybb->input['message'],
        "ipaddress" => get_ip(),
        "posthash" => $mybb->input['posthash']
    );

    $posthandler->set_data($post);

    // Now let the post handler do all the hard work.
    $valid_post = $posthandler->validate_post();

    // Mark thread as read
    require_once MYBB_ROOT."inc/functions_indicators.php";
    mark_thread_read($mybb->input['tid'], $mybb->input['fid']);
    $postinfo = $posthandler->insert_post();

    echo get_latest_post($mybb->input['tid']);
}

请帮忙!

1 个答案:

答案 0 :(得分:3)

变化:

data : "message="+message+"&posthash="+posthash+"&lastpid="+lastpid+"&tid="+tid+"&fid="+fid,

为:

data : "message="+encodeURIComponent (message)+"&posthash="+posthash+"&lastpid="+lastpid+"&tid="+tid+"&fid="+fid,

并且您应该全部设置 - &符号需要使用encodeURIComponent正确转义,因为它是一个特殊字符。

另一种选择是将data作为对象传递,而不是自己创建字符串。

data: { message: message, posthash: posthash, lastpid: lastpid, tid: tid, fid: tid }