1

How to call PHP class method by ajax with jQuery But No need any PHP handler file.

This is my PHP Class animal.php:-

<?php

class animal
{     
  function getName()
  {
    return "lion";
  }
}
?>

jQuery:-

<script type=text/javascript>
  $.ajax({
    type: "POST",
    data: {
      abc:animal;
    },
    url: "animal.php",
    dataType: "html",
    async: false,
    success: function(data) {
      alert(data);
    }
  });    
</script>

I want to use directly call getName() method in jquery no need any other handler file between PHP Class and jQuery.

Parameter data will be any thing but after success I require "lion" in success data.

Please Help.

6
  • 2
    You can't really do this without a new instance of the object animal. Some sort of "handler" will be required. Commented Apr 11, 2016 at 7:45
  • you need middelware to execute php class Commented Apr 11, 2016 at 7:45
  • I am pretty sure you can't. Which URL do you think it would access when accessing a PHP method? I can only think of a solution by creating a check which POST or GET parameters are sent which results in executing a specific method. Commented Apr 11, 2016 at 7:46
  • Is it possible we can create instance in jQuery and call method. Commented Apr 11, 2016 at 7:47
  • 2
    @AbidHussain jQuery is Javascript, it's client-side, it executes on your visitor's computer. PHP is server-side, it executes on your server. jQuery has no idea that there's a server with a script on it, all it knows is it's environment. And PHP doesn't know about jQuery/Javascript, or even the user. Which is why you need a handler php script, that would handle the request, and based on it it would create an instance of your class, call the required method and echo the results. Commented Apr 11, 2016 at 7:54

1 Answer 1

1

Your PHP file is nothing more, but just a class, a definition of an object. Which doesnt really gets "executed" (does nothing, but just defines) unless you create an instance of that class. You cannot achieve it, what ever you are trying to achieve, in the way you would like to. But here's an alternate example, where your class would create an instance of itself and call a method based on what you send with AJAX:

Add method to jQuery like:

<script type=text/javascript>
  $.ajax({
    type: "POST",
    data: {
      'animal':animal,
      'method':"getName"
    },
    url: "animal.php",
    dataType: "html",
    async: false,
    success: function(data) {
      alert(data);
    }
  });    
</script>

and then in PHP:

class Animal
{     
  function getName()
  {
    return "lion";
  }
}
(new Animal)->{$_POST['method']}();

Note: class member/method access on instantiation has been added in PHP 5.4

Reference

EDIT

Your class method returns "lion", but to php, to server-side and NOT to client-side, so we have to output something like with echo or var_dump() functions..

function getName()
{
   echo "lion";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.