AJAX request and PHP class functions

后端 未结 8 870
小鲜肉
小鲜肉 2020-12-08 05:29

How to call a PHP class function from an ajax call

animal.php file

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


        
8条回答
  •  粉色の甜心
    2020-12-08 05:57

    You need one additional script, because your animal class can't do anything on its own.

    First, in another script file, include animal.php. Then make an object of the animal class - let's call it myAnimal. Then call myAnimal->getName() and echo the results. That will provide the response to your Ajax script.

    Use this new script as the target of your Ajax request instead of targeting animal.php.

提交回复
热议问题