How to Call PHP Function in a Class from HTML Button Click

元气小坏坏 提交于 2019-12-04 09:30:44

You should do something like that

<button class="btn" onclick="onrequest();">Add</button>

function onrequest() {
      $.post( 
          'example.php'
       ).success(function(resp){
            json = $.parseJSON(resp);
            alert(json);
       });
}

and in example.php call your function

$class = new Awards();
$method =  $class->clickFunction();
echo json_encode($method);

and in your clickFunction();

clickFunction(){
 $array = array(
   'status'  => '1'
 );    
 return $array; 
}

first of you have to a instance of class which is like that

$class = new Awards();

then if you want to onclick event you should make a javascript code but how to get the function you can do like this

<?php echo $class->clickFunction(); ?>

I think you can't call a class directly from HTML in PHP you have to call it through HTTP by loading a page.

Hence, use a AJAX call to call Awards.php. Eg. with JQuery it could look like this $.get("Awards.php", function() { alert( "success" ); })

In your php-file you should also call your class something like this <?php echo $class->clickFunction(); ?>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!