Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a function in php file this function i want to access in html file, if the user clicks button i need to execute the function

What I have tried:

<button type="button" class="btn btn-info" onclick="fbshdifg()">gkhk</button>
                           <button type="button" class="btn btn-info" onclick="gtretyretre()">C</button>
Posted
Updated 13-Aug-19 9:35am
v3

1 solution

Your HTML is viewed in a client (browser), and the function you want is in PHP on the server.

You basically have 2 options on how to do this:
1. Place your button within a FORM to be processed on the server
2. Call a javascript function which utilizes AJAX to call the server script for processing.

Reference:
html - How to call a PHP function on the click of a button - Stack Overflow[^]

Updated for code samples

AjaxFunctions.php
This file has two parts:
_Part 1 sees if there is an action to perform and if so which to do
_Part 2 contains the various functions that can be called
PHP
<?php
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'PDF':
                DownloadPDF();
                break;
            case 'TXT':
                DownloadTxt();
                break;
        }
    }

    function DownloadPDF() {
        // Your PDF code
        exit;
    }

    function insert() {
        // Your TXT code
        exit;
    }
?>
 
Share this answer
 
v2
Comments
Member 14558025 13-Aug-19 16:00pm    
lets take example function as "update()" is in my php file, how can i make ajax call for this in js file
MadMyche 13-Aug-19 16:18pm    
The first 4 or 5 answers on that S/O page are all valid for your scenario, you will just need to change the names in those answers to match your naming
Member 14558025 13-Aug-19 16:24pm    
Thanks for your reply MadMyche sir.
Member 14558025 13-Aug-19 16:38pm    
i want to call specific function in the php file how can i do?
Member 14558025 13-Aug-19 16:38pm    
i followed s/o but it is not resulting me as expected

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900