I’m trying to create a module that uses Ajax on the backend. Since this has a Control Panel backend, I need to do ajax calls to the module. I’ve tried unsuccessfully to generate calls to the module using EE links and constants, but I keep running against roadblocks. The generated links have semicolons, which in turn generate ‘Disallowed Character’ errors.
Ex: In my javascript code
var link = <?= BASE. AMP .'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=featured'.AMP.'method=find_franchise'; ?>No matter how I code it, even when I specifically use the ‘&’ character, EE strips it out and replaces it with &
Suggestions?
This prolly is not best way to do this, but I added another action in the exp_actions table. In the upd.my_module.php file I have the following:
$data = array(
'module_name' => 'My_module' ,
'module_version' => $this->version,
'has_cp_backend' => 'y',
'has_publish_fields' => 'n'
);
$this->EE->db->insert('modules', $data);
// For exp_actions
$data = array('class' => 'My_module', 'method' => 'i_love_ajax');
$this->EE->db->insert('actions', $data);This allows you to use then use some JS like this:
$.post(BASE_URL, {
ACT: $(new_option).find('input[name="ee_polls_ajax_add_option_action_id"]').val(), // Action ID
my_module: 'is cool',
i_am: 'also cool'
}, function(data) {
console.log〈data〉;
}, 'json');(“BASE_URL” is a JS variable with the base URL of the site)
However, by doing this, you let anyone access this i_love_ajax method. Inside this method you must make sure that the current user is allowed to access. You can do this by checking the current member group ID.
this->session->userdata('group_id')If the group ID is 1, then you got a super admin and they can access it for sure. If its not 1 you should check the module_member_groups table.
There is probably a way better way of doing this… but its the only thing I could come up with.
I’m not sure if this applies or not, but here is an overview of making AJAX calls within the CP. You can solve your original problem with using some JS variables EE generates.
http://forrst.com/posts/ExpressionEngine_Control_Panel_AJAX-OZJ
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.