0

所以我有这个代码片段。

function get_box_ajax() {
    global $box;

    $box = get_box(); // creates box object

    ob_start();
    get_template( '/filepath/content.php' );
    $output = ob_get_clean();
}

// in the content.php file

global $box;

<form action="<?php echo box_url( $box->url ); ?>" method="post"> // error on this line
...
</form>

因此,使用此代码,我在调用 $box->url 时遇到非对象错误。请注意,这是通过 ajax 完成的。

所以我认为在我的 ajax 函数中我已经全球化了 $box 并且这将需要但它似乎不起作用?有什么想法吗?

4

2 回答 2

1

两件事情:

什么时候调用你的get_box_ajax函数?该功能有什么作用get_box?这两件事都是相关的。

我认为问题不在于是否box是全局的(它是),而是是否正在定义url变量或者是否正在初始化。boxbox

于 2013-07-31T23:48:52.443 回答
0

在函数之前将 box 设置为 null。全局范围内没有引用

$box = null;
function get_box_ajax() {
    global $box;

    $box = get_box();

把开头改成那个

于 2013-07-31T23:32:59.960 回答