Using Ajax to update content [solved]

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Using Ajax to update content [solved]

Post 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?
Last edited by kidcardboard on Fri Oct 01, 2010 6:29 pm, edited 1 time in total.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Using Ajax to update content

Post by Nullig »

Could it be this line?

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

Should it be?

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

Nullig
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Using Ajax to update content

Post by calguy1000 »

I would not rely on xajax for long in CMSMS.
We're phasing it out.

jquery is sooo much better.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Using Ajax to update content

Post 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?
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Using Ajax to update content

Post by calguy1000 »

yes, jquery is coming in CMSMS 1.9
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Using Ajax to update content

Post 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?
NaN

Re: Using Ajax to update content

Post 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?
Last edited by NaN on Wed Sep 29, 2010 12:02 am, edited 1 time in total.
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Using Ajax to update content

Post 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.
NaN

Re: Using Ajax to update content

Post 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".
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Using Ajax to update content

Post 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
NaN

Re: Using Ajax to update content

Post 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;

User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Using Ajax to update content

Post by kidcardboard »

Thank you... that worked perfectly
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: Using Ajax to update content

Post by Dr.CSS »

Post Reply

Return to “Developers Discussion”