php - Accessing controller's global variable in ajax returns NULL -
hi guys have controller proceses data in index() assign on global variable.
i want global variable accessed ajax when pages loaded.
here i've done in index():
class search extends ss_controller { public static $q; public function index(){ $k = $this->input->get(null, true); $data['title'] = "search"; $data['page_content'] = "search_results_view.php"; $data['logout'] = "/./ssmis/home/logout"; $data['active_nav'] = 'search'; $data['k'] = $k['k']; self::$q = array('123','456'); ...
and have method function called in ajax:
public function q(){ var_dump(self::$q ); if(self::$q ){ $response['error'] = false; $response['has_data'] = true; $response['message'] = 'success'; $response['data'] = $this->q; $this->echo_response($response,200,'ok!'); } else { $response['error'] = true; $response['message'] = 'no results returned'; $this->echo_response($response,200,'not ok!'); } }
the problem var_dump(self::$q ); returns null.
how can value of $q assigned in index() of controller?
thanks!
you're mixing terms. in code $q
class static variable, not global variable - defined using keyword global
before var name, e.g. global $foo
. note: using global
in 2015 very bad idea™ - not this.
secondly, don't seem understand how request-response works. if call index method sets variable, once script completes execution, self::$q
no longer exists. ajax request isn't pixie dust, it's regular http request - new request. brand-new request route calls q()
not have called index()
before, no code setting self::$q
ever executed, ajax request's point of view. in other words, variable set on course of single request exists until request has returned response. if want persist, need store value of $q
in form of persistent storage - database, user's session, cookies or that.
Comments
Post a Comment