Page 1 of 1

xajax not working

Posted: Mon Aug 17, 2009 3:08 pm
by irish
Has anyone figured out how to use xajax for front-end development? I can get xajax to work in the admin area but when I tried to use it on the action.default.php page, it does work correctly. The ajax call will bring back the entire page plus the xml request.

I have added the methods per this topic: http://forum.cmsmadesimple.org/index.ph ... 909.0.html but it didn't help.

Here is the code I have so far:

Code: Select all

require_once($_SERVER['DOCUMENT_ROOT'] .'/lib/xajax/xajax.inc.php');
$xajax = new xajax();
$xajax->registerFunction('myFunction');

$xajax->processRequests();
$this->smarty->assign('js',$xajax->getJavascript($config['root_url'] . '/lib/xajax')."\n");

function myFunction($arg)
{
    // do some stuff based on $arg like query data from a database and
    // put it into a variable like $newContent
    $newContent = "Value of $arg: ".$arg;
    
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    
    // add a command to the response to assign the innerHTML attribute of
    // the element with id="SomeElementId" to whatever the new content is
    $objResponse->addAssign("IntroRt", "innerHTML", $newContent);
    
    //return the  xajaxResponse object
    return $objResponse->getXML();
}

Re: xajax not working

Posted: Mon Aug 17, 2009 3:37 pm
by irish
Never mind, I just got it working. I can't believe I spent days trying to get this to work and as a last resort posted this topic here. After posting the topic, I went back to it and tried a few more things eventually found the answer:

Code: Select all

require_once($_SERVER['DOCUMENT_ROOT'] .'/lib/xajax/xajax.inc.php');
$xajax = new xajax();
//$xajax->registerFunction('ajaxGetIntro');
$xajax->registerFunction('myFunction');

$xajax->processRequests();
$this->smarty->assign('js',$xajax->getJavascript($config['root_url'] . '/lib/xajax')."\n");

function myFunction($arg)
{
    
$handlers = ob_list_handlers(); 
for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }

	// do some stuff based on $arg like query data from a database and
    // put it into a variable like $newContent
    $newContent = "Value of $arg: ".$arg;
    
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    
    // add a command to the response to assign the innerHTML attribute of
    // the element with id="SomeElementId" to whatever the new content is
    //$objResponse->assign("IntroRt","innerHTML", $newContent);
    $objResponse->addAssign("IntroRt", "innerHTML", $newContent);
    
    //print_r($objResponse->getXML());
    
    //return the  xajaxResponse object
    return $objResponse->getXML();
}

Notice the "ob_end_clean" in the function.

Re: xajax not working

Posted: Mon Aug 17, 2009 3:44 pm
by calguy1000
add showtemplate=false to the URL.

Re: xajax not working

Posted: Mon Aug 17, 2009 3:48 pm
by irish
calguy? what do you mean?

It's javascript that calls the Ajax using an onclick event.

Re: xajax not working

Posted: Mon Aug 17, 2009 3:54 pm
by calguy1000

Code: Select all

        /*
                Constructor: xajax

                Constructs a xajax instance and initializes the plugin system.

                Parameters:

                sRequestURI - (optional):  The <xajax->sRequestURI> to be used
                        for calls back to the server.  If empty, xajax fills in the current
                        URI that initiated this request.
        */
so grab the current request URI, add &showtemplate=false to the end of it... pass it to the xajax constructor.

Re: xajax not working

Posted: Mon Aug 17, 2009 4:08 pm
by irish
hhhmmmm, Interesting. However, it brings back all the content of the page and not just the xml response.