JQuery AJAX and module

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
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

JQuery AJAX and module

Post 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
Last edited by nervino on Sun Mar 20, 2011 4:03 pm, edited 2 times in total.
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

Re: JQuery AJAX and module

Post by Jos »

I think this topic has some good tips:
http://forum.cmsmadesimple.org/viewtopi ... jax+module
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: JQuery AJAX and module

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

Re: JQuery AJAX and module

Post 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.
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: JQuery AJAX and module

Post by nervino »

Thank you!
Post Reply

Return to “Developers Discussion”