-1

I don't know what is wrong in my code but when I ever I try to post a message with & in it on any line or word, it stops on that point and post only the post message text before of that &.

For example I'm using this Post Message: Lorem Ipsum dolor & set imet If I post this then it posts only this Lorem Ipsum dolor and not the whole sentence.

My jQuery is:

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']);
}

Please help!

user2854563
  • 268
  • 1
  • 11
  • 25

1 Answers1

3

Change:

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

to:

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

and you should be all set - the ampersand needs to be properly escaped with encodeURIComponent, as it's a special character.

The other option is to pass data as an object rather than creating the string yourself.

data: { message: message, posthash: posthash, lastpid: lastpid, tid: tid, fid: tid }
ceejayoz
  • 176,543
  • 40
  • 303
  • 368