Skip to main content
Topic: Adding cusotm button php (Read 1427 times) previous topic - next topic

Adding cusotm button php

Hi Team
Its look good just lot documentation lacking

for example can you pleae provide me one example

add a button in list page and when click it call my api that will dow something

just an example is enough

 

Re: Adding cusotm button php

Reply #1
@muhdkhokhar‍ please click on the custom checkbox next to the list page you want to work on, there you can write you HTML code for the button and javascript function that will be triggered by the button. Here is a similar thread with a sample code and explanation. https://phprad.com/forum/index.php?topic=1012.msg3352#msg3352, https://phprad.com/forum/index.php?topic=910.msg2998#msg2998, https://phprad.com/forum/index.php?topic=540.msg1720#msg1720.

Adding the following sample to your list page should work.
Code: [Select]
<button class="btn btn-primary" onclick="callmyapi();">My sample Button</button>

<script>
function callmyapi(){  
    //We now make an ajax post to the server
    var request = $.ajax({
        type: 'GET', // the type of request we are making.
        url: siteAddr+'api/MysampleApi/?csrf_token='+csrfToken, //our api link/url
        cache: false, // we dont want the request to be cached.
    });
  
    // Now we check for a response that will be gotten from the server.
    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        alert(response.msg);
    });
  
    // Now we check for errors we get when trying to make our post request.
    request.fail(function (jqXHR, textStatus, errorThrown){[size=1]});[/size]
</script>

And the following code should be added to your ApiController.php
Code: [Select]
function MysampleApi(){
        //perform your actions here and return result.
        $res['msg'] = "We got result from our sample API.";
        render_json($res); // here we print result as json
}