outrunthewolf
outrunthewolf

Reputation: 383

Accessing a PHP class function through AJAX

Does anyone know how I can directly access a function from a PHP class through AJAX (using jQuery).

PHP:

class Blah
{
    function __construct()
    {
    }

    function doSomething(data)
    {
        echo "I am not an animal";
    }
}

jQuery:

$.ajax({
        url: myClass.php,
        data: Blah.doSomething(); "or" Blah->doSomething()  "or whatever"
    });

I know this is a crude example, i'm just trying to illustrate a point, I hope you can get the gist of my question.

At the moment i'm doing something along these lines:

$.ajax({
        url: myClass.php,
        data: data : { 'type':'doSomething' }
    });

||

if(POST['data']['type'] == 'doSomething')
{
     $this->doSomething();
}

And I don't like it...

Upvotes: 0

Views: 5667

Answers (6)

Joel Candela
Joel Candela

Reputation: 11

**Your class**
class Blah {
    function doSomething(data) {
        echo "I am not an animal, I'm a ".data;
    }
}

**Ajax call in other document**
$.ajax({
   url: example.php,
   data: info='beast'
   success: data () { ...
});

**in example.php**
include (class.blah.php);
$obj = new SomeClass();
$obj ->doStuff($_GET['info']);

Upvotes: 1

rajesh
rajesh

Reputation: 2523

i don't know why do you like to do so. its not a good programming practice. but you can try something like this

$.ajax({
    url: myClass.php,
    data: {'call':'Blah.doSomething', 'args':5}
});

and in server side you can do like

$fun = explode('.', $call);
$function = array($fun[0], $fun[1]);
if (is_callable($function)) {
    $response = call_user_func_array($function, $args);
}
echo $response;

Upvotes: 3

Stelian Matei
Stelian Matei

Reputation: 11623

You need to create an object of that class and then invoke the methods you need. The class declaration doesn't not execute any code.

For example, you can add this below the class:

class Blah { ... }
$b = new Blah();
$b->doSomething();

UPDATE:

If you want to invoke the method sent in POST, you can use the function call_user_function:

$method_name = $_POST['method'];
$b->{$method_name}();

See here more details: http://php.net/manual/en/function.call-user-func.php

Upvotes: 5

Greg Kramida
Greg Kramida

Reputation: 4244

I concur with mazzucci's answer. I hope this is a bit more complete.

PHP:

class SomeClass{ 
 //definintion 
}
$obj = new SomeClass();
$obj->doStuff();

HTML:

<input type="button" onclick="<?php $obj->doStuff(); ?>" id="btnAdd" value="Click Me" />

Also, you can look into xajax, a nice little framework that simplifies php<->javascript interaction. It can call js functions from php as well. Here's the link: http://www.xajax-project.org/en/home/

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174977

It's not possible. What you currently have is probably the best way.

Upvotes: 0

James Lin
James Lin

Reputation: 26538

Right now I can't think of any better way than PHP outputting results in json and you get the result via getJSON

Upvotes: 1

Related Questions