-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.

8
  • 2
    I can't get your question, really :( Commented Jul 5, 2016 at 16:23
  • Hmm...sorry. Basically I want to alter the solution posted in stackoverflow.com/questions/21304054/… by "Agha Umair Ahmed" so that I can specify to access any method in the Awards.php page (stock-c.php in my situation). Commented Jul 5, 2016 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. Commented Jul 5, 2016 at 16:29
  • 1
    php.net/manual/en/functions.variable-functions.php Have you seen this, you could combine it with a get request. @user3889963 Commented Jul 5, 2016 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. Commented Jul 5, 2016 at 16:30

1 Answer 1

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.

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.