MyBB Community Forums

Full Version: get_post and build_postbit don't work when using AJAX
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm using this code (hooked into xmlhttp) to re-render a post after an AJAX call completes:

if ($mybb->get_input('action') == 'spell_cast') {
        $sid = (int)$mybb->get_input('sid');
        $pid = (int)$mybb->get_input('pid');
        $extra_details = json_decode($mybb->get_input('extra_details'), true);
        $return = array(
            "success" => false,
            "html" => NULL
        );
        try {
            $spellbook->castSpell($sid, $pid, $extra_details);
            $return['success'] = true;
            // Re-render post
            require_once(MYBB_ROOT.'inc/functions.php');
            require_once(MYBB_ROOT.'inc/functions_post.php');
            $post = get_post($pid);
            $return['html'] = build_postbit($post);
        } catch (Exception $e) {
            $return['html'] = ($e->getMessage());
        }

        echo json_encode($return);
    }

Here is the error returned in the Javascript console:

<br />
<b>Parse error</b>:  syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in <b>C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\minus-world-mybb\inc\functions_post.php(843) : eval()'d code</b> on line <b>8</b><br />
{"success":true,"html":null}

It seems there are a bunch of variables missing, causing the eval for postbit_classic to fail. But I don't understand why get_post($pid) isn't doing what it's supposed to (the pid is definitely correct). Could anyone help with this?

EDIT:
get_post isn't even returning an array like it's supposed to. This is what it returns (random post):
22107134602210620RE: THE GREEN GARRISON WILL RISE AGAIN01Super Mario1484091422[font=Comic Sans MS]who hacked smarty's account[/font]「�>810001100

I also tried using this instead of get_post:
$query = $db->simple_select("posts", "*", "pid = '{$pid}'");
$post = $db->fetch_array($query);

Same result. There's no reason that shouldn't return an array, so what's going on?