We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47622
    • 9 Posts
    Hi Guys,

    I want to use Ajax to pull xpdo requests from the server.

    When I click on submit, the following script is executed on the server:
    <?php
     
    define('MODX_CORE_PATH', '/var/www/html/raf/core/');
    define('MODX_CONFIG_KEY','config');
    
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
     
    $host = 'localhost';
    $username = 'admin';
    $password = 'p4ssword';
    $dbname = 'dbase1';
    $charset = 'utf-8';
     
    $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
    $xpdo = new xPDO($dsn, $username, $password);
     
    echo $o = ($xpdo->connect()) ? 'Connected' : 'Not Connected';
        
    $results = $xpdo->query("SELECT id FROM some_table");
    $recordCount = $results->rowCount();
    print $recordCount;
    


    Unfortunately the result is always "Not Connected".

    [ed. note: ohlala last edited this post 8 years, 6 months ago.]
    • Usually the best way to use AJAX is to write a snippet that does what you want, then put that snippet in a resource with no template. Use the resource's URL as the AJAX URL. That way you have all of the MODX API at your convenience.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 47622
        • 9 Posts
        Thank you Susan !

        By chance does someone have an example of snippet?

        Because I can't get it working...

        Even in a resource without template, my [[!snippet]] produces "Not Connected"