doubi7346 2013-07-31 23:30
浏览 24

PHP如何利用ajax获取内容时全局变量不起作用?

So I have this code snippet.

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>

So with this code, I am getting a non-object error on the call to $box->url. Take note this is done via ajax.

So I thought in my ajax function I have already globalized $box and that will take but it doesn't seem to work? Any thoughts?

  • 写回答

2条回答 默认 最新

  • duannei0044 2013-07-31 23:32
    关注

    Set box to null before the function. There's no reference in the global scope

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

    Change the start to that

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)