Click here to Skip to main content
14,265,813 members
Rate this:
Please Sign up or sign in to 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 4 days ago
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
    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;
    }
?>
   
v2
Comments
Member 14558025 4 days ago
   
lets take example function as "update()" is in my php file, how can i make ajax call for this in js file
MadMyche 4 days ago
   
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 4 days ago
   
Thanks for your reply MadMyche sir.
Member 14558025 4 days ago
   
i want to call specific function in the php file how can i do?
Member 14558025 4 days ago
   
i followed s/o but it is not resulting me as expected
MadMyche 4 days ago
   
Really can't help you without the actual code
Member 14558025 4 days ago
   
Let us take an example: PHP file name "sample.php" in that two functions is there, I have created a two buttons in html file one is "download in PDF format " button and other is download in text format, if the user click first button then first function needs to call and if the user clicks second button second function needs to call
Member 14558025 4 days ago
   
I tried my best using ajax, but I can't get it
Member 14558025 4 days ago
   
Simply if the user clicks the button then it needs to call first function which is in PHP file

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100