We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

how to implement ajax in custom module backend

Development and Programming

ahmedeng's avatar
ahmedeng
30 posts
14 years ago
ahmedeng's avatar ahmedeng

Hello,

I can’t implement ajax in custom module backend that is my mcp file code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class hijri_date_mcp {

    /**
     * Constructor
     *
     * @access    public
     */
    function __construct()
    {
        // Make a local reference to the ExpressionEngine super object
        $this->EE =& get_instance();
        // $this->index();
        
    }

    // --------------------------------------------------------------------

    /**
     * Main Page
     *
     * @access    public
     */
    function index()
    {
        $this->EE->load->library('javascript');
        $this->EE->load->library('table');
        $this->EE->load->helper('form');

        $ajax_method="adjust_date";
        $m=str_replace("amp;","",BASE);
        $this->EE->javascript->output(array(
'var days=0;

function adjust_date(value)
{ 
sSource="'.$m.'&C=addons_modules&M=show_module_cp&module=hijri_date&method;='.$ajax_method.'";
         
$.getJSON( sSource,                 {    
              value: value
         },
         function(result) {    
      alert(result);
         $("#date").html(result); 
        });                   
}
$("#plus").click(function(){
    adjust_date(1);
});              

$("#minus").click(function(){
    uadjust_date(-1);
});
'            )
        );
            
        $this->EE->javascript->compile();
        
        $this->EE->cp->set_variable('cp_page_title', $this->EE->lang->line('hijri_date_module_name'));

        $vars['action_url'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=hijri_date'.AMP.'method=adjust_date';
        $vars['form_hidden'] = NULL;
        
        return $this->EE->load->view('index', $vars, TRUE);
    }
    

    function adjust_date()
    {   
      
      $value = $this->EE->input->get_post('value');          
      $content=file_get_contents(PATH_THIRD."hijri_date/uCal.class.php");
      
      preg_match("/var \\\$adjustment_value=([0-9]{1,});/",$content,$match);
      $prev_value=$match[1];
      $value=$value+$prev_value;
      
      $content=str_replace("var \$adjustment_value=".($prev_value).";","var \$adjustment_value=$value;",$content);
      $f=fopen(PATH_THIRD."hijri_date/uCal.class.php",'w');
      fwrite($f,$content);
      fclose($f);
      
      include PATH_THIRD.'hijri_date/uCal.class.php';
      $d=new uCal;
      $d->setLang('ar');
      echo $hijri_date=$d->date("l j F Y");
    }

    
    
}
// END CLASS

The ajax made successful but i didn’t get response callback. when i replace “getJSON” with “post” i get response callback but i got the control panel home page in the response.

thanks,

[Mod Edit: Moved to the Development and Programming forum]

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.