Welcome Guest, Not a member yet? Register   Sign In
XAJAX Test_Function is not defined
#1

[eluser]phpuser[/eluser]
Hello,

I've added XAJAX library following the instructions from the XAJAX perfect setup article. All seems fine except when I try to run the test_function. Here is the code of the controller :
Code:
function test_function($number) {
      $objResponse = new xajaxResponse();
      $objResponse->assign("omeElementId","innerHTML", "Xajax is working. Lets add: ".($number+3));
      return $objResponse->_printResponse_XML();
    }
    
    function showTemplate() {
      $this->load->library('xajax');
      $this->xajax->registerFunction("test_function");  
      $this->xajax->processRequest();
      $template['xajax_js'] = $this->xajax->getjavascript(base_url());
      $template['content'] = '<div id="SomeElementId"></div>&lt;input type="button" value="test"&gt;';
      $this->load->view('template', $template);
  }
Using firebug it returns the error "test_function is not defined".

If I try to call the test_function directly throught the browser it works fine, for example "http://127.0.0.1/myweb/index.php/mycontroller/test_function/3" it returns :
"SXajax is working. Lets add: 6"

Please, if anyone can help me with this.
Thanks !
Joseph
#2

[eluser]InsiteFX[/eluser]
Show your complete controller.

InsiteFX
#3

[eluser]phpuser[/eluser]
Hello,

I think there's nothing special, just the class definition and the index method.

Code:
class mycontroller extends Controller {

function mycontroller(){
  parent::Controller();
  session_start();

  $this->load->model('mymodel','',TRUE);
}

function index(){    
  $this->load->view('home');
}

function test_function($number) {
  $objResponse = new xajaxResponse();
  $objResponse->assign("omeElementId","innerHTML", "Xajax is working. Lets add: ".($number+3));
  return $objResponse->_printResponse_XML();
}
    
function showTemplate() {
  $this->load->library('xajax');
  $this->xajax->registerFunction("test_function");  
  $this->xajax->processRequest();
  $template['xajax_js'] = $this->xajax->getjavascript(base_url());
  $template['content'] = '<div id="SomeElementId"></div>&lt;input type="button" value="test"&gt;';
  $this->load->view('template', $template);
}  
}

Thanks !
#4

[eluser]InsiteFX[/eluser]
It Could be a problem with the sessions and the ajax call.
You may need to play with this for xajax.

Create the MY_Session file below.
1) CI 1.7.2 - Place in application/libraries/MY_Session
2) CI 2.0 - Place in application/core/MY_Session

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* ------------------------------------------------------------------------
* CI Session Class Extension.
* ------------------------------------------------------------------------
*
* Add the following define to the bottom of application/config/constants.php
* // Define Ajax Request
* define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
*
*/

class MY_Session extends CI_Session {
   /*
    * Do not update an existing session on ajax calls
    *
    * @access    public
    * @return    void
    */
    public function sess_update()
    {
        if ( ! IS_AJAX)
        {
            parent::sess_update();
        }
    }

    function sess_destroy()
    {
        parent::sess_destroy();

        $this->userdata = array();
    }

}

// ------------------------------------------------------------------------
/* End of file MY_Session.php */
/* Location: ./application/libraries/MY_Session.php */

InsiteFX
#5

[eluser]phpuser[/eluser]
Hello,

first of all, thanks for your help. I've tried your suggestions (add the MY_Session unit into libraries folder and also the define into constants.php, but I got the same error "test_function" is not defined.
I don't understand why the MY_Session is needed or where to use it.

In any case, some hours later I've found where was the problem, it was a silly thing, first of all I've verified that all xajax files had the ?&gt; and no empty lines at the end of files, then also on my controllers, and finally I notice that in the example of xajax article had the "xajax_" word before the name of the function that must be called (see below) :
Code:
$data['content'] = '<div id="SomeElementId"></div>&lt;input type="button" value="test"&gt;';

Finally fixed !

Many thanks




Theme © iAndrew 2016 - Forum software by © MyBB