views:

54

answers:

1

I need some help with this please I can't get a handle on it.

The problem is that I want to call a class method, in this case with static methods with an ajax call.

I have put the helper class in the same folder as the script that is called by ajax for easy referencing and try to include it. Could it be that my refencing is wrong?

If I make a testclass in the file that is called by ajax I can get a response.

class test {
    public function testit() {
     return "testit";
    }
}
$t=new test;
$check= $t->testit();



switch($action) {
case "someaction":
    $data = array();
    $file='input_helper.php';
    include_once $file;

    $check= input_helper::ip_address();
    header('Content-type: application/json');

    $output = array(
     "check" => $check,
     "user" => $data
     );

    echo json_encode($output);

    exit(0); // Stop script.

    break;
//...

EDIT FOR MORE CLARIFICATION

The action is set as a post variable in the ajax function The ajax url points to a script that takes some action based on the posted variables

thanks, Richard

+1  A: 
<?php
  include_once 'your/class/path/helper_class.php';
  .
  .

at the top of your PHP page should do it. it really has nothing to do with AJAX. If your PHP file is in fact being hit on the callback, then that should work properly.

Optionally, to test that your path is correct, if you do:

 <?php 
     require 'your/class/path/helper_class.php';
     .
     .

If the path is not correct PHP will throw a fatal E_ERROR level error.

jaywon
or perhaps: include_once 'your/class/path/input_helper.php'; It's more concrete, and include_once is not a function.
Don
thanks, I finally cleaned out the whole class and only put the testit() in it and it works. Something in the class must have caused it. Just for clarity, spl_register does not work anymore I guess if I call the script without the www.domain controller/function methodology. Or should I give that url to the ajax call, so that it will automaticly instantiates the objects again?
Richard
in other words, create an ajax controller with methods and do a $.ajax({url: "www.domain/ajaxcontroller/testit",
Richard
@Don, oops, thanks for catching that, changes duly noted :)
jaywon
@Richard - yes I think creating a controller to handle that functionality is a good idea
jaywon
I try'd that, the problem is that the frontcontroller is also instantiated and it creates html output in the response(firebug). Because at the end off the index I have echo $frontctrl->getBody()How can you fire an ajax controller without causing this? I have to look into that.
Richard