Page 1 of 1

JQuery AJAX and module

Posted: Sun Mar 20, 2011 2:08 pm
by nervino
Hi All, I'm trying to make ajax working in my module, but with poor success and a lot of unsolved (for me..) questions.

(My goal: submit a form and update a db table with ajax, then refresh form fields).

I don't think I'll have problem with db etc., but what is not clear to me is how to use a jquery call from a module page without affect security, because with the JQuery call I loose all benefits of function SetParameters() etc.


Example code:

In action.default.php I have

Code: Select all

echo $this->ProcessTemplate('ajax.tpl');

In ajax.tpl:

Code: Select all

$.ajax({
	 type: "POST",
	 url: "http://www.cms.vmware/modules/MyModule/action.ajax.php",
	dataType : 'json',
(...)
$('#message').text(data.msg).show(0);
(...)
});

<div id="message"></div>

In action.ajax.php

Code: Select all

$return['error'] = false;

$return['msg'] = 'OKKKK !!';
echo json_encode($return);
In this way, I can't see the echoed "$return" in my action.default.php.

If I put on the top of action.ajax.php the following code, all works fine:

Code: Select all

require '../../include.php'; // (from site's root )

$db=&$GLOBALS["gCms"]->db;

global $gCms;

if (!isset($gCms)) exit;

$mymodule = &$gCms->modules['MyModule']['object'];

Now, I'd like to know if this is a correct way to use ajax in cmsms, and if, on your opinion, there may be problems about security.

I've read all I've found in this forum and calguy1000's site about the use of ajax in cmsms; and I also tried to use ajaxMS module, studying also Chat module, but I wasn't be able to make ajax work in my module...

thank you

Re: JQuery AJAX and module

Posted: Sun Mar 20, 2011 4:00 pm
by Jos
I think this topic has some good tips:
http://forum.cmsmadesimple.org/viewtopi ... jax+module

Re: JQuery AJAX and module

Posted: Sun Mar 20, 2011 4:26 pm
by nervino
Thank you Jos.
I had read that post and tried to use the CreateLink function as suggested, but it doesn't work for me

action.default.php

Code: Select all

$ajax_params = array('disable_theme'=>'true','showtemplate' => 'false');
$ajurl=$this->CreateLink($id, 'ajax', $returnid, '', $ajax_params, '', true, true);

ajax.tpl

Code: Select all

$.ajax({
type: "POST",
url: "{/literal}{$ajurl}{literal}",
(...)
Also, what does it mean that jquery is implemented in CMSMS 1.9?
Is it available only for the admin panel?

In the front-end I have to include it in the template or it does not work.

Re: JQuery AJAX and module

Posted: Fri Apr 01, 2011 4:02 pm
by kidcardboard
nervino wrote: Also, what does it mean that jquery is implemented in CMSMS 1.9?
Is it available only for the admin panel?

In the front-end I have to include it in the template or it does not work.
Yes, it is available in the admin panel and yes you have to include it yourself in your templates to use it in the front end. Just include lib/jquery/js/jquery-1.4.2.min.js

There's a couple other libraries/plugins in that dir too so have a look there (ie jquery.json-2.2.js) in case you want to use those too.

Re: JQuery AJAX and module

Posted: Sat Apr 02, 2011 10:57 am
by nervino
Thank you!