fart-y-goer
fart-y-goer

Reputation: 739

Trigger PHP function by clicking HTML link

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!

Upvotes: 6

Views: 71653

Answers (5)

gotofritz
gotofritz

Reputation: 3381

(EDIT although this works, it's a bad idea as it is. One should never read from $_GET without sanitising it first)

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'] );
}
?>

Upvotes: 8

jilt
jilt

Reputation: 43

I found this code in a plugin, they have user a foreach look to trigger the action:

$actions = unset($meta[$key]);
foreach ( $actions as $action => $value ) {
    echo '<li><a href="' . esc_url( $value['url'] ) . '" class="my-action-' . $action . '">' . '<i class="fa fa-times"></i></a></li>';
}

Upvotes: 0

Zack Zatkin-Gold
Zack Zatkin-Gold

Reputation: 824

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>

Upvotes: 11

Dominic Green
Dominic Green

Reputation: 10260

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

Upvotes: 3

powerbuoy
powerbuoy

Reputation: 12838

HTML

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

PHP

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

Upvotes: 8

Related Questions