简体   繁体   中英

call php function with onclick event

php_file:-

class class_name{
     function method1(){
         //some code
         }
     function method2(){
         //some code
         }
}

in html:-

<i class="fa fa-angle-right" onclick=""></i>

How can i call function from php file when the icon clicked

A jquery demo is as follows:-

abc.php:- (you can change extension to .html also if you are not going to use any php code in this file)

<button class="fa fa-angle-right" onclick="myFunction();">Click Me Please!</button>

<script src= "https://code.jquery.com/jquery-3.1.1.min.js"></script>

<script type="text/javascript">
function myFunction(){

     $.ajax({url: "demo_test.php", success: function(result){
        alert(result);
    }});
}
</script>

demo_test.php:-

<?php
include_once('def.php');

$obj = new myClass();

$result = $obj->method1();

echo $result;
?>

def.php:-

<?php
class myClass{
    function method1(){
        echo "demo";
    }
    function method2(){
        echo "demo2";
    }
}

Output:- http://prntscr.com/clw7hu and http://prntscr.com/clw7sl

Note:-

All three files must be at same working location(same directory)

It's a very small and easy sample to understand what is exactly need and how it can performed. Thanks

You can't, you need jQuery to do this.

Example:

<i class="banana"></i>

<script type="text/javascript">
    $(document).ready(function() {
        $(".banana").click(function() {
             // write action you want to do here
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM