We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9241
    • 42 Posts
    I’ve been searching all over the net and cannot find a solution that actually works, so I’m trying it here...

    The problem I have is that I have a page from which I am making an ajax call when the user clicks on a button. Now upon this ajax call some changes are made to some session variables. For some reason I am unable to update/create php session variables using ajax calls.

    I’ve checked the session id’s for both the page and the resulting ajax calls and both are the same.

    My setup is that I have a document with a snippet in which handles the ajax request, the snippet, for simplicity reasons, currently only has this in it:
    if (empty($_SESSION['count']))
        $_SESSION['count'] = 1;
    
    $_SESSION['count'] += 1;
    
    return $_SESSION['count'];


    On completion of the ajax request the response is just shown on the page. Now one would think, each time I click the button it would show, 1, then 2, 3, 4, etc as I click the button, but it doesn’t, it just keeps on showing 1. I browse directly to the ajax page/resource which I call in the javascript, then it shows, 2, 3, 4, etc. as I refresh, and when I go back to the page with the button and click on the button, it just shows the last number I saw from directly loading the page, in this case 4.

    I don’t understand why it does not want to update the session variables when using an ajax call, anybody have ANY idea what is causing this or how to fix it? It’s fairly urgent and I’m getting balder by the minute embarrassed
      • 22629
      • 194 Posts
      modx caches snippet calls - so it’s highly likely it’s cached the "1" and now won’t update it.

      Is your snippet call in your document cached [[MySnippet]] or uncached [[!MySnippet]]?
        Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

        modx Revolution 2.2.6
        Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

        Content-Managed Websites Built on MODX
        • 9241
        • 42 Posts
        Quote from: Pandy06269 at Oct 28, 2010, 07:26 AM

        modx caches snippet calls - so it’s highly likely it’s cached the "1" and now won’t update it.

        Is your snippet call in your document cached [[MySnippet]] or uncached [[!MySnippet]]?

        I turned of caching where possible for the snippet and document, I’m calling it as uncached [[!MySnippet]]
          • 26903
          • 1,336 Posts
          Try a print_r of your $_SESSION in your test snippet, what do you see?
            Use MODx, or the cat gets it!
            • 9241
            • 42 Posts
            Quote from: shamblett at Oct 28, 2010, 08:13 AM

            Try a print_r of your $_SESSION in your test snippet, what do you see?

            That isn’t really going to help, but I will somehow try and get a printout of the session variable seeing as the snippet only returns a value and doesn’t echo/print anything because it’s only used for ajax calls to return values...
              • 59
              • 10 Posts
              Hi EvoBunny, did you end up finding a fix for your problem?

              I am having a similar problem using jQuery AJAX.

              I have set everything to not cache, but the session variable remains the same as the first time it is set (first time snippet is called).
                • 9241
                • 42 Posts
                Did managed to solve it, cannot remember what we did exactly, not sure if it just started working or whether it was a typo error in the end... but I noticed with with ajax requests it sends a modx session id now.
                  • 16501
                  • 28 Posts
                  Argh! You are so evil, I am having this exact same problem, grrr! smiley
                    • 33983 ☆ A M B ☆
                    • 181 Posts
                    Revo wraps the session vars so if you have a custom PHP page that is not within MODX you will not beable to access the session data. To get access to the session data you need to initialize MODX. Here is the code to initialize MODX that needs to be on your custom page:

                    <?php
                        // load MODX to beable to read the session info
                        define("MODX_BASE_PATH", "Path to your modx install!" );
                        define('MODX_CORE_PATH', MODX_BASE_PATH . 'core/');
                        define('MODX_MANAGER_PATH', MODX_BASE_PATH . 'manager/');
                        define('MODX_CONNECTORS_PATH', MODX_BASE_PATH . 'connectors/');
                        define('MODX_ASSETS_PATH', MODX_BASE_PATH . 'assets/');
                        // urls
                        define("MODX_BASE_URL", "Your URL");
                        define("MODX_MANAGER_URL", MODX_BASE_URL.'manager/');
                        define("MODX_ASSETS_URL", MODX_BASE_URL.'assets/' );
                        define("MODX_CONNECTORS_URL", MODX_BASE_URL.'connectors/');
                            
                        include_once MODX_CORE_PATH . 'model/modx/modx.class.php'; 
                        $modx= new modX();
                        $modx->initialize('web');
                    ?>


                    Now you can call on your session vars.