【问题标题】:php ajax call from class returns an empty array来自类的 php ajax 调用返回一个空数组
【发布时间】:2014-04-29 14:20:07
【问题描述】:

我正在使用 ajax POST 调用函数,然后尝试访问类中的对象,但由于某种原因,我在调用类函数后得到了 NULL

这是我的代码

include_once dirname(__FILE__).'/../db/_mysql.php';
include_once dirname(__FILE__).'/../class/door.php';

$db = new _mysql();

if (isset($_POST['door'])) {
    if ($_POST['door'] == 'get-default') {
        $doors = array();
        for ($i = 0; $i < $_POST['amount']; $i++) {
            array_push($doors, array(
                'door-id' => $i,
                'panel-colors' => 'valkoinen'
                )
            );
        }
        order_door::setDoors($doors);
    }

    if ($_POST['door'] == 'get-doors') {
        print_r(order_door::getDoors());
        $doors = order_door::getDoors();
        if ((int) $_POST['total-count'] > count($doors)) {
            echo $_POST['total-count'] . '>' . count($doors);
        } else if ((int) $_POST['total-count'] < count($doors)) {
            echo $_POST['total-count'] . '<' . count($doors);
        }
    }
}

class order_door {

    private static $doors;

    public static function getDoors() {
        return self::$doors;
    }
    public static function setDoors($array) {
        if (count($array) == 0) {
            self::$doors = array();
        } else {
            self::$doors = $array;
            print_r(self::getDoors());
        }
    }

}

在第二个 ajax 调用中,我试图访问 get-doors 在 POST get-doors print_r(order_door::getDoors());没有打印任何东西。有什么想法吗?

【问题讨论】:

  • 你的 ajax 请求在哪里

标签: php jquery ajax class


【解决方案1】:

实际上这是必须发生的,你没有任何数据

private static $doors;

因此,您在一个 ajax 请求中设置它并在另一个请求中调用它会将其重置为默认值。

要解决此问题,您必须将此数据保存到 SESSION 例如。

【讨论】:

  • 将我的数组存储到 SESSION 中是个好主意。为什么我没想到 =P 谢谢!但只是想知道是否也可以使用 ajax.. 还是每次都将数组重置为 null
猜你喜欢
  • 2019-04-10
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多