Page 1 of 1

Using Ajax to update content [solved]

Posted: Fri Sep 24, 2010 9:00 pm
by kidcardboard
I've being trying to implement a similar feature to the page active state done via ajax. I've got it making the post but it fails from there. This is what I have in my module (unrelated code has been removed):

Code: Select all

require_once(dirname(dirname(dirname(__FILE__))) . '/lib/xajax/xajax_core/xajax.inc.php');

function GetHeaderHTML() {
	$xajax = new xajax();
	$xajax->register(XAJAX_FUNCTION,'setProjectActiveState');
	$xajax->processRequest();
	return $xajax->getJavascript($config['root_url'] . '/lib/xajax')."\n";
}


function setProjectActiveState($projectId) {
	$objResponse = new xajaxResponse();
		
	$project = $this->getProjectsById($projectId);
		
	$project->setIsActive(!$project->getIsActive());
		
	$this->updateProject($project);
		
	$objResponse->assign("contentlist", "innerHTML", display_content_list());
	return $objResponse;
}


function display_content_list() {
	$img = ($project->getIsActive()) ? 
	$gCms->variables['admintheme']->DisplayImage('icons/system/true.gif', $this->Lang('settoinactive'),'','','systemicon') : 
	$gCms->variables['admintheme']->DisplayImage('icons/system/false.gif', $this->Lang('settoactive'), '','','systemicon') ;
	$thisurl = "../Modules/ProjectManagerProjectManager.module.php";
	$list[$i]['isActive'] = "<a href=\"$thisurl&setProjectActiveState=".$project->getId()."\" onclick=\"xajax_setProjectActiveState(".$project->getId().");return false;\">".$img."</a>";
}
and the error i'm getting in my server logs is:

Code: Select all

PHP Warning: call_user_func_array() [<a href='function.call-user-func-array'>function.call-user-func-array</a>]: First argument is expected to be a valid callback, 'setProjectActiveState' was given in /path/to/root/v2/lib/xajax/xajax_core/plugin_layer/support/xajaxUserFunction.inc.php on line 230, referer: http://lightafuse.ca/v2/admin/moduleinterface.php?sp_=ae7903c6&module=ProjectManager 
Does anyboby have any idea what I'm doing wrong?

Re: Using Ajax to update content

Posted: Fri Sep 24, 2010 11:05 pm
by Nullig
Could it be this line?

$thisurl = "../Modules/ProjectManagerProjectManager.module.php";

Should it be?

$thisurl = "../Modules/ProjectManager/ProjectManager.module.php";

Nullig

Re: Using Ajax to update content

Posted: Fri Sep 24, 2010 11:14 pm
by calguy1000
I would not rely on xajax for long in CMSMS.
We're phasing it out.

jquery is sooo much better.

Re: Using Ajax to update content

Posted: Mon Sep 27, 2010 6:49 pm
by kidcardboard
@Nulling: Thanks for noticing that, but that didn't fix it

@callguy1000: Thank for the heads up. Is jQuery going to be implemented into CMSMS or will it be up to the developer to include it for each module/project?

Re: Using Ajax to update content

Posted: Mon Sep 27, 2010 7:09 pm
by calguy1000
yes, jquery is coming in CMSMS 1.9

Re: Using Ajax to update content

Posted: Mon Sep 27, 2010 8:06 pm
by kidcardboard
fantastic... i've got it making the ajax request through jquery now, but i'm stuck trying to figure out how to instantiate the cms/module so i can make use of it... or am i gonna have to do all my db calls the old school way by hand?

Re: Using Ajax to update content

Posted: Tue Sep 28, 2010 11:52 pm
by NaN
There is a function called GetModuleInstance();
Just pass the name of the module you want to get:
Example;

Code: Select all


$mod =& $this->GetModuleInstance('News');

Take a look in the API docs for more info about module functions.

Anyway are you refering to your module class directly?

Code: Select all


$thisurl = "../Modules/ProjectManager/ProjectManager.module.php";

This doesn't make any sense to me.
I would create a link using the CMSms module API passing urlonly=true that will link to a certain module action:

Code: Select all


$myActionUrl = $this->CreateLink($id, 'myAction', $returnid, 'click me', array('disable_theme'=>true','showtemplate'=>'false', ... ),'', true);

This will create a link that processes your module action. So you don't need to instanciate anything.
The params 'disable_theme'=>true' and 'showtemplate'=>'false' causes the CMS to not render anything else but just your module output. (no template in frontend, no backend theme)

Or did i get you wrong?

Re: Using Ajax to update content

Posted: Wed Sep 29, 2010 10:34 pm
by kidcardboard
I don't think you fully understood what I meant, but it got me thinking. I was trying to call the module instead of just using an action url, like $this->CreateLink(...), to make my Ajax call to. Now the call is made, and the data is updated in the db, but I'm having issues updating the data on screen. I either get the entire page loaded into just the div I want to update, or if I set:

Code: Select all

$params['disable_theme'] = true;
Then I get what I want to display, but then I can't make calls to:

Code: Select all

$gCms->variables['admintheme']->DisplayImage(...);
to get theme specific items.

Re: Using Ajax to update content

Posted: Wed Sep 29, 2010 10:49 pm
by NaN
Ah, well, this is something i did not think of.
If theme is disabled there is no 'admintheme'.

Why don't you put the images in your template wrapped in hidden divs with an id for each icon when the module is called the first time?
Instead of just returning the result of your module return an XML result that contains two elements: the id of the image to display and the message to display.
So you can use jQuery to select the icon you need to display "here" and the message to display "there".

Re: Using Ajax to update content

Posted: Thu Sep 30, 2010 5:11 pm
by kidcardboard
So I've decided to go the xml route, but I've noticed that there's some markup tagged on to the end of what I'm returning.

I return:

Code: Select all

$output = "<project>";
$output .= "<id>".$project->getId()."</id>";
$output .= "</project>";
print $output;
And I get:

Code: Select all

<project><id>1</id></project></div><!-- admin theme disabled -->
<__body>
</__html>

<!-- 0.0049439999999999 /  / 372012 / 7824408 -->
even if I do something as simple as

Code: Select all

echo 1;
is still adds the markup

Re: Using Ajax to update content

Posted: Thu Sep 30, 2010 5:31 pm
by NaN
I would do it this way:

Code: Select all


@ob_end_clean();
@ob_start();
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<project>';
echo '<id>';
echo '<![CDATA[';

echo $project->getId();

echo ']]>';
echo '</id>';
echo '</project>';
ob_end_flush();
exit;


Re: Using Ajax to update content

Posted: Thu Sep 30, 2010 10:29 pm
by kidcardboard
Thank you... that worked perfectly

Re: Using Ajax to update content

Posted: Fri Oct 01, 2010 1:46 am
by Dr.CSS