Trigger PHP function by clicking HTML link

65,230

Solution 1

You can do this by means of loading the entire page over again by the use of form submission, or by loading specific page contents directly into the page without needing to go from page to page. The second method is called "AJAX" (Asynchoronous Javascript and XML). Here are two examples, one of each specified.

Form submission approach

form.php

<?php
function get_users(){
}
if(isset($_GET['get_users']))
{
    get_users();
}
?>
...
<form method="get">
    <input type="hidden" name="get_users">
    <input type="submit">
</form>

AJAX approach

ajax_file.php

<?php
function call_me(){
    // your php code
}
call_me();
?>

form.html

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            // do something if the page loaded successfully
        }
    }
    xmlhttp.open("GET","ajax_file.php",true);
    xmlhttp.send();
}
</script>
</head>
<body>
<a href="#" onclick="loadXMLDoc()">click to call function</a>
</body>
</html>

Solution 2

HTML

<a href="?list_me">list me</a>

PHP

<?php
if (isset($_GET['list_me'])) listMe();

Solution 3

You can pass it as a query parameter of the link. http://example.com/?command=listMe&username=tom However that way everybody will be able to run the function by loading that URL

<a href="http://example.com/?command=listMe&">List me</a>

and in the PHP

<?php
if( isset($_GET['list_me']) && isset($_SESSION['Username'] ) ){
   listMe( $_SESSION['Username'] );
}
?>

Solution 4

To trigger a function on link click with php the only way I know would be to append a param in the url of the link and then listen for that

<a href="?function">Add my username to the list</a>

Then check for link

if (isset($_GET['function'])){
      runFunction();
}

This is because php is a server side technology if you want to fire something without refreshing the page you would need to look at something like javascript

Share:
65,230
fart-y-goer
Author by

fart-y-goer

I am new to programming

Updated on November 11, 2020

Comments

  • fart-y-goer
    fart-y-goer over 3 years

    Is it possible to trigger a PHP function by just clicking a link? Or should I stick with the form submit method? If clicking the link will work, where should the link refer to?

    Here is a sample code:

    <?php
        session_start();
        function listMe($username){
            $username = mysql_real_escape_string($username);
            $query = mysql_query("INSERT INTO List (Usernames) VALUES ('$username')") or die(mysql_error());
        }
    ?>
    
    <html>
        <head>
            <title>SAMPLE</title>
        </head>
        <body>
            <a href="**__???___**">Add my username to the list</a>
            <?php 
                listMe($_SESSION['Username']); 
            ?>
        </body>
    </html>
    

    Maybe someone can point me in the right direction. Thanks!

  • fart-y-goer
    fart-y-goer over 12 years
    I'm tweaking your code, I'll feedback once it worked. btw I think I can use your code for other cases.. Thank you!
  • Zack Zatkin-Gold
    Zack Zatkin-Gold over 12 years
    No problem. I left out little pieces that I assume you could fill in yourself, and to prevent myself from writing the whole application for you.
  • Nguai al
    Nguai al over 4 years
    i think you are missing some code in your anchor. you need href="xyz.php?list_me='me'