-1

I'm trying to develop a webpage (menu.php) which has a <div> when clicked accesses "one" of the many methods in the stock-c.php. The code I have developed as follows:

menu.php

<?php 
require_once '../control/stock-c.php';
?>
<html>
<body>
    <div onclick="<?php $obj=new StockController(); $obj->checkAccessRights();?>">  //I know this is a wrong implementation
       //some code here
    </div>
</body>
</html>

stock-c.php

class StockController{
    public function checkAccessRights(){
        //some code here
    }
    public function getPID(){
        //some code here
    }
}

I know menu.php file's onclick event is a wrong implementation.

Since there will be other situations that I will need to call other methods (like getPID()) in other PHP web pages, I'm not sure if using jQuery to create an AJAX call on the onclick event in menu.php to access an "altered" stock-c.php file such as:

class StockController{
    $obj=new StockController();
    $obj->checkAccessRights();

    public function checkAccessRights(){
        //some code here
    }
    public function getPID(){
        //some code here
    }
}

which was suggested in How to Call PHP Function in a Class from HTML Button Click would work as this breaks my ability to access the other function. I just am not sure what alternatives I have. Any help will be appreciated.

Community
  • 1
  • 1
user3889963
  • 477
  • 3
  • 6
  • 17
  • 2
    I can't get your question, really :( – Chay22 Jul 05 '16 at 16:23
  • Hmm...sorry. Basically I want to alter the solution posted in http://stackoverflow.com/questions/21304054/how-to-call-php-function-in-a-class-from-html-button-click# by "Agha Umair Ahmed" so that I can specify to access any method in the Awards.php page (stock-c.php in my situation). – user3889963 Jul 05 '16 at 16:27
  • 2
    You will have to do an ajax call to a php script that calls your method, which is what that other question is suggesting. – Jonathan Kuhn Jul 05 '16 at 16:29
  • 1
    http://php.net/manual/en/functions.variable-functions.php Have you seen this, you could combine it with a get request. @user3889963 – Adam Forbis Jul 05 '16 at 16:29
  • not possible. PHP runs on the server. You can **NOT** invoke php code by clicking on something in the client's browser. At most you can do an ajax request/form submission will will end up running PHP code on the server. – Marc B Jul 05 '16 at 16:30
  • @Adam Forbis...Are you suggesting I use ajax to send a post variable to stock-c.php so that depending on the value sent it will invoke the necessary method? Or were you suggesting something even more better that that?? – user3889963 Jul 05 '16 at 16:47
  • @user3889963 that is pretty much it. – Adam Forbis Jul 05 '16 at 17:01
  • Thanks...:-) I think you are the only one who understood my question. Thanks for your help man...:-) – user3889963 Jul 05 '16 at 17:09

1 Answers1

1

One way to call different methods in a script would be to use PHP's variable functions. This would allow calling the script with a get or post request and use the input to select which function to call.

From php.net:

<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar';
        $this->$name(); // This calls the Bar() method
    }

    function Bar()
    {
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()

?>

For security reasons it may be a good idea to put something like a switch statement or other way of limiting input to keep a user from invoking arbitrary functions.

Adam Forbis
  • 461
  • 3
  • 11